Python子进程Popen参数区别

发布于 2024-10-05 06:53:12 字数 325 浏览 0 评论 0原文

有什么区别

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-10-12 06:53:12

第一个实际上只运行 cat (因为如果您使用 shell = True 则解析命令的方式),因此挂起,因为它正在等待输入。第二个是运行cat /path/to/file。您可以通过以下方式看到这一点:

>>> subprocess.Popen(['ls', '-l'])
<subprocess.Popen object at 0x10048bdd0>
>>> total 8
-rw-------  1 root     wheel  652 Nov 29 09:07 000d94cfc78b4
srwxr-xr-x  1 nbastin  wheel    0 Nov 29 09:06 ics179
srwxr-xr-x  1 nbastin  wheel    0 Nov 29 09:06 icssuis501
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-3ZniHd
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-8QRgz2
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-M5ppWp
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launchd-137.ztQAmI
drwx------  2 nbastin  wheel   68 Nov 29 09:57 ssh-LreGlOZPAR

与使用 shell = True 进行比较:

>>> subprocess.Popen(['ls', '-l'], shell = True)
<subprocess.Popen object at 0x10048bd90>
>>> 000d94cfc78b4       ics179          icssuis501      launch-3ZniHd       launch-8QRgz2       launch-M5ppWp       launchd-137.ztQAmI  ssh-LreGlOZPAR

如果设置 shell = True,则将列表作为 args 传递将不会'不能得到你想要的行为 - 你需要传递一个字符串。

The first one is actually only running cat (because of the way that the command is parsed if you use shell = True), and thus hanging because it's waiting for input. The second one is running cat /path/to/file. You can see this by doing:

>>> subprocess.Popen(['ls', '-l'])
<subprocess.Popen object at 0x10048bdd0>
>>> total 8
-rw-------  1 root     wheel  652 Nov 29 09:07 000d94cfc78b4
srwxr-xr-x  1 nbastin  wheel    0 Nov 29 09:06 ics179
srwxr-xr-x  1 nbastin  wheel    0 Nov 29 09:06 icssuis501
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-3ZniHd
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-8QRgz2
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launch-M5ppWp
drwx------  3 nbastin  wheel  102 Nov 29 09:06 launchd-137.ztQAmI
drwx------  2 nbastin  wheel   68 Nov 29 09:57 ssh-LreGlOZPAR

Versus doing it with shell = True:

>>> subprocess.Popen(['ls', '-l'], shell = True)
<subprocess.Popen object at 0x10048bd90>
>>> 000d94cfc78b4       ics179          icssuis501      launch-3ZniHd       launch-8QRgz2       launch-M5ppWp       launchd-137.ztQAmI  ssh-LreGlOZPAR

If you set shell = True, passing a list as args won't get you the behaviour you want - you need to pass a string.

尴尬癌患者 2024-10-12 06:53:12

我测试了这些示例,发现第一个可以在 ipython 中返回 Popen 对象,但第二个也可以,但打印了 cat: /path/to/file: No such file or directory

我想避免使用尽可能使用 subprocess.Popenshell=True 并使用列表 ['x', '--version'] 因为这样可以避免需要引用路径名,其中包含“坟墓”或

pymp.py 补丁中的一些此类内容,我将:更改

p = subprocess.Popen(['mplayer -slave -quiet \'' + target + '\' -include \'' + MPLAYERCONFFILE + '\' 2>/dev/null'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

为:

devnull = open('/dev/null', 'w')
p = subprocess.Popen(['mplayer', '-slave', '-quiet', target, '-include', MPLAYERCONFFILE], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=devnull)

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 with shell=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 muck

in a patch to pymp.py i changed:

p = subprocess.Popen(['mplayer -slave -quiet \'' + target + '\' -include \'' + MPLAYERCONFFILE + '\' 2>/dev/null'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

to:

devnull = open('/dev/null', 'w')
p = subprocess.Popen(['mplayer', '-slave', '-quiet', target, '-include', MPLAYERCONFFILE], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=devnull)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文