为什么是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
当您传入列表时,只有第一项被视为命令。其余部分被视为用于运行命令的shell的参数。从Python docs:
相关问题 更多 >
编程相关推荐