2

why python's subprocess.Popen() behavior is odd when running shell command as string and as list? when should we pass shell command as string and as list?

>>> 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
Murali Mopuru
  • 6,086
  • 5
  • 33
  • 51

1 Answers1

2

When you pass in a list only the first item is treated as a command. The rest is treated as arguments to the shell used to run the command. From the 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], ...])

julius383
  • 101
  • 1
  • 4