【问题标题】:Run command pipes with subprocess.Popen使用 subprocess.Popen 运行命令管道
【发布时间】:2009-10-31 14:48:45
【问题描述】:

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

mysqldump database_name table_name | bzip2 > filename

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

【问题讨论】:

    标签: python command pipe


    【解决方案1】:

    您希望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
    

    【讨论】:

      最近更新 更多