os模块
os.system方式:
import os
os.system('top')
os.system('cat /proc/cpuinfo')
说明
os.open方式:
import os
output = os.popen('df')
print output.read() #如果命令自身无报错,则返回正确的值
说明
commands模块
使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:
a = commands.getoutput('ps -ef ')
b = commands.getstatusoutput('vmstat')
subprocess模块
说明
subprocess.call(["some_command","some_argument","another_argument_or_path")
from subprocess import call
call(['ls','-l','/boot','/sys']) #
call('ls -a /',shell=True)
subprocess.Popen(command,shell=True)
class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False,
startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())
subprocess.Popen类的实例的一些方法:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有