
在Python语法中有哪些自带的系统命令,这些命令如何调用?下面web建站小编给大家简单介绍一下!
os.popen方法
import os
result = os.popen('cat /etc/passwd')
print(result.read())
os.popen()方法不仅执行命令而且返回执行后的信息对象
import os
result = os.system('cat /etc/passwd')
print(result)      # 0
os.system方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息。
import subprocess
res = subprocess.Popen('cat /etc/passwd', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 使用管道
print res.stdout.read()  # 标准输出
for line in res.stdout.readlines():
  print line
res.stdout.close()         # 关闭
subprocess是一个功能强大的子进程管理模块,是替换os.system,os.spawn*等方法的一个模块。
import commands
status = commands.getstatus('cat /etc/passwd')
print(status)
output = commands.getoutput('cat /etc/passwd')
print(output)
(status, output) = commands.getstatusoutput('cat /etc/passwd')
print(status, output)
Mac电脑安装Python软件后怎么查看版本号(附下载地址)
标签: commands模块, os.popen, os.system, Python系统命令, python语法, subprocess模块
上面是“Python语法中有哪些系统命令(如何调用)”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_4590.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

 吃鸡游戏对于内存的要求高吗?简单介绍16g和双16g的区别
 CodiumAI:一款专注于代码完整性和质量的AI代码测试工具