使用python子进程运行shell命令

时间:2018-03-05 11:30:54

标签: python shell subprocess

为什么python的subprocess.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

1 个答案:

答案 0 :(得分:0)

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

  

在POSIX上,shell = True,shell默认为/ bin / sh。如果args是   string,string指定通过shell执行的命令。   这意味着字符串必须完全按照原样进行格式化   在shell提示符下键入时。这包括,例如,引用或   反斜杠转义文件名,其中包含空格。如果args是   sequence,第一项指定命令字符串,以及任何   其他项目将被视为shell的附加参数   本身。也就是说,Popen相当于:

     

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