Python中执行系统命令常见的几种方法

目录

Python执行系统命令的常用方法整理笔记。

os.system()

在子终端运行系统命令,不返回执行结果。

import os
os.system('ls')

os.popen()

执行命令并返回信息对象,便于程序处理。

import os
tmp = os.popen('ls *.sh').readlines()
# tmp: ['install_zabbix.sh\n', 'manage_deploy.sh\n', ...]

subprocess 模块

支持线程控制和监控,返回结果,推荐使用。

import subprocess
subprocess.call(["cmd", "arg1", "arg2"], shell=True)

p = subprocess.Popen('ls *.sh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(p.stdout.readlines())
for line in p.stdout.readlines():
    print(line,)
retval = p.wait()

commands 模块

常用方法:getoutput() 返回输出,getstatusoutput() 返回状态码和输出。

import commands
commands.getoutput('ls *.sh')
# 'install_zabbix.sh\nmanage_deploy.sh\n...'

commands.getstatusoutput('ls *.sh')
# (0, 'install_zabbix.sh\nmanage_deploy.sh\n...')

注意:处理中文时建议使用 subprocess,os.popen 会出错。

#python