使用subprocess.Popen运行命令管道

jac*_*ack 4 python command pipe

如何使用以下命令运行subprocess.Popen

mysqldump database_name table_name | bzip2 > filename
Run Code Online (Sandbox Code Playgroud)

我知道os.system()可以做这个工作,但我不想等待转储完成主程序.

Ros*_*ers 6

您希望该shell=True选项使其执行shell命令:

import subprocess
subprocess.Popen("sleep 4s && echo right thar, right thar",shell=True);
print 'i like it when you put it'
Run Code Online (Sandbox Code Playgroud)

产量:

 I like it when you put it
 [4 seconds later]
 right thar, right thar
Run Code Online (Sandbox Code Playgroud)