使用python子进程运行shell命令

2024-09-30 05:18:03 发布

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

为什么是Python子流程.Popen()当以字符串和列表形式运行shell命令时,行为是奇怪的?我们什么时候应该以字符串和列表的形式传递shell命令?你知道吗

>>> import subprocess
>>> p = subprocess.Popen('du -sh *', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
>>> stdout, _ = p.communicate()
>>> print stdout[:1000]
244M    bin
3.1M    bmps
14M     buildweb
10M     contrib
4.0M    gobuild
168K    gobuild-starter-kit
468K    gobuild-test
3.6M    lib
46M     machines
1.4M    machineweb
75M     microservices
1.4M    monthly_maintenance
20K     perforce
9.1M    scheduler
196K    service
2.5M    statusweb
516K    storage
228K    symindex
7.2M    webapps

>>> p = subprocess.Popen('du -sh *'.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
>>> stdout, _ = p.communicate()
>>> print stdout[:1000]
8       ./statusweb/infrastructure/management/commands
12      ./statusweb/infrastructure/management
16      ./statusweb/infrastructure
48      ./statusweb/statusweb/settings
56      ./statusweb/statusweb
164     ./statusweb/dashboard/migrations
8       ./statusweb/dashboard/tests/fixtures
64      ./statusweb/dashboard/tests
12      ./statusweb/dashboard/static/dashboard/img
12      ./statusweb/dashboard/static/dashboard/css
48      ./statusweb/dashboard/static/dashboard/js/status
12      ./statusweb/dashboard/static/dashboard/js/spec
72      ./statusweb/dashboard/static/dashboard/js
100     ./statusweb/dashboard/static/dashboard
104     ./statusweb/dashboard/static
36      ./statusweb/dashboard/templates/dashboard/dust
32      ./statusweb/dashboard/templates/dashboard/emails
88      ./statusweb/dashboard/templates/dashboard
8       ./statusweb/dashboard/templates/admin/dashboard/issue
8       ./statusweb/dashboard/templates/admin/dashboard/maintenance
20      ./statusweb/dashboard/templates/admin/dashboard
28      ./statusweb/dashboard/templates/admin
8       ./statusweb/dashboard/templates/registra

Tags: 字符串列表adminstdoutjsstaticshellinfrastructure
1条回答
网友
1楼 · 发布于 2024-09-30 05:18:03

当您传入列表时,只有第一项被视为命令。其余部分被视为用于运行命令的shell的参数。从Python docs

On POSIX with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

相关问题 更多 >

    热门问题