代码1:以参数序列的形式传递linux命令
from subprocess import Popen, PIPE
run_cmd = Popen(['ls','-l','mkdir','hello'], stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode
输出-1:
^{pr2}$在上面的代码中,我尝试运行多个linux命令,方法是将它们作为参数序列传递。如果我把上面的代码修改为下面的代码,它可以正常工作。在
代码2:将linux命令作为字符串传递
from subprocess import Popen, PIPE
run_cmd = Popen('mkdir hello; ls -l; echo Hello; rm -r hello', shell=True, stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode
输出-2:
drwxrwxr-x. 2 sujatap sujatap 6 May 9 21:28 hello
-rw-rw-r--. 1 sujatap sujatap 53 May 8 20:51 test.py
Hello
0
由于shell=True
不是建议使用的方法,所以我想使用前一种方法运行linux命令。谢谢。在
如果有什么不起作用,请检查它的文档:https://docs.python.org/2/library/subprocess.html#popen-constructor
因此,首先测试您的单个程序的运行情况(程序及其参数的列表),然后列出一个列表,并使用循环按顺序运行它们:
相关问题 更多 >
编程相关推荐