>>> import subprocess
>>> a = subprocess.run('ls') 这样终端只会打印出执行命令结果
0.10.4性能测试报告.docxcert_问题复现
>>> a
CompletedProcess(args='ls', returncode=0) #returncode=0 表示执行成功。
>>> subprocess.run('ls') 如果直接写subprocess.run('ls') 那么直接在终端打出CompletedProcess(args='ls', returncode=0) 和 执行命令结果
0.10.4性能测试报告.docxcert_问题复现
CompletedProcess(args='ls', returncode=0)
>>> subprocess.run('ls -al',shell=True) # 需要交给Linux shell自己解析,则:传入命令字符串,shell=True
---------
执行错误
>>> subprocess.run('ll',shell=True) #mac不支持ll
/bin/sh: ll: command not found
CompletedProcess(args='ll', returncode=127) #returncode != 0 就是执行失败
>>> subprocess.run('ll') #不加shell=True直接报错 所以还是加上shell=True
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'll': 'll'