Python子进程Popen参数区别
有什么区别
subprocess.Popen(['cat','/path/to/file'], stdout=subprocess.PIPE, shell=True)
和 和
subprocess.Popen(['cat '+'/path/to/file'], stdout=subprocess.PIPE, shell=True)
? 我正在 ipython 中执行此操作。 对于第一个,ipython 只是挂起。也许不会挂起,但速度要慢得多。 第二个就OK了。
只是不知道为什么。
What's the difference between
subprocess.Popen(['cat','/path/to/file'], stdout=subprocess.PIPE, shell=True)
and
subprocess.Popen(['cat '+'/path/to/file'], stdout=subprocess.PIPE, shell=True)
?
I'm doing this in ipython.
For the first one, ipython just hang. Maybe not hang, but it's significantly slower.
The second is just OK.
Just don't know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个实际上只运行
cat
(因为如果您使用shell = True
则解析命令的方式),因此挂起,因为它正在等待输入。第二个是运行cat /path/to/file
。您可以通过以下方式看到这一点:与使用
shell = True
进行比较:如果设置
shell = True
,则将列表作为args
传递将不会'不能得到你想要的行为 - 你需要传递一个字符串。The first one is actually only running
cat
(because of the way that the command is parsed if you useshell = True
), and thus hanging because it's waiting for input. The second one is runningcat /path/to/file
. You can see this by doing:Versus doing it with
shell = True
:If you set
shell = True
, passing a list asargs
won't get you the behaviour you want - you need to pass a string.我测试了这些示例,发现第一个可以在 ipython 中返回 Popen 对象,但第二个也可以,但打印了
cat: /path/to/file: No such file or directory
我想避免使用尽可能使用
subprocess.Popen
和shell=True
并使用列表['x', '--version']
因为这样可以避免需要引用路径名,其中包含“坟墓”或pymp.py 补丁中的一些此类内容,我将:更改
为:
i tested the samples and found that the first worked returning a Popen object in ipython but the second did the same but printed
cat: /path/to/file: No such file or directory
i like to avoid using
subprocess.Popen
withshell=True
when possible and use a list['x', '--version']
as this avoids the need to quote path names with say ` graves in them or some such muckin a patch to pymp.py i changed:
to: