如何使用子流程popen Python

2024-04-26 12:50:55 发布

您现在位置:Python中文网/ 问答频道 /正文

由于os.popen被subprocess.popen替换,我想知道如何转换

os.popen('swfdump /tmp/filename.swf/ -d')

到subprocess.popen()

我试过:

subprocess.Popen("swfdump /tmp/filename.swf -d")
subprocess.Popen("swfdump %s -d" % (filename))  # NOTE: filename is a variable
                                                # containing /tmp/filename.swf

但我想我写得不对。任何帮助都将不胜感激。谢谢


Tags: isosfilenamevariabletmpnotesubprocesspopen
2条回答

使用sh,会让事情变得简单得多:

import sh
print sh.swfdump("/tmp/filename.swf", "-d")

subprocess.Popen接受参数列表:

from subprocess import Popen, PIPE

process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()

甚至还有一个section of the documentation专门用来帮助用户从os.popen迁移到subprocess

相关问题 更多 >