赞
踩
看看他的api都能干些啥
>>> subprocess.call(['ls','-l'])
total 8
drwxrwxr-x 4 ws ws 4096 Nov 25 13:50 MonitorSystem
drwxrwxr-x 2 ws ws 4096 Feb 19 10:09 tmp
subprocess.check_call() 方法
我们说过call执行返回一个状态码,我们可以通过check_call()函数来检测命令的执行结果,如果不成功将返回 subprocess.CalledProcessError 异常
>>> try:
subprocess.check_call("ls -t", shell=True)
except subprocess.CalledProcessError as err:
print("Command Error")
/bin/sh: lt: command not found
Command Error
subprocess.check_output() 方法
call()方法启动的进程,其标准输入输出会绑定到父进程的输入和输出。调用程序无法获取命令的输出结果。但可以通过check_output()方法来捕获输出。
>>> output=subprocess.check_output("ls -l",shell=True)
>>> output
b'total 8\ndrwxrwxr-x 4 ws ws 4096 Nov 25 13:50 MonitorSystem\ndrwxrwxr-x 2 ws ws 4096 Feb 19 10:09 tmp\n'
>>> print(output.decode('utf-8'))
total 8
drwxrwxr-x 4 ws ws 4096 Nov 25 13:50 MonitorSystem
drwxrwxr-x 2 ws ws 4096 Feb 19 10:09 tmp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。