Why subprocess.Popen doesn’t work when args is sequence?

From the documentation

On Unix, with shell=True: […] 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], ...])

Which translates in your case to:

Popen(['/bin/sh', '-c', 'du', '-s', '-b', maildir])

This means that -s, -b and maildir are interpreted as options by the shell, not by du (try it on the shell commandline!).

Since shell=True is not needed in your case anyway, you could just remove it:

size = subprocess.Popen(['du', '-s', '-b', maildir],
                    stdout=subprocess.PIPE).communicate()[0].split()[0]

Alternatively you could just use your orignal approach, but you don’t need a list in that case. You would also have to take care of spaces in the directory name:

size = subprocess.Popen('du -s -b "%s"' % maildir, shell=True,
                    stdout=subprocess.PIPE).communicate()[0].split()[0]

Leave a Comment