使用subprocess.Popen运行命令管道

时间:2009-10-31 14:48:46

标签: python command pipe

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

mysqldump database_name table_name | bzip2 > filename

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

1 个答案:

答案 0 :(得分: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'

产生:

 I like it when you put it
 [4 seconds later]
 right thar, right thar