[Tutor] subprocess.call list vs. str argument
Peter Otten
__peter__ at web.de
Mon Feb 24 21:54:08 CET 2014
Albert-Jan Roskam wrote:
> Hi,
>
> In the code below, cmd1 and cmd2 are equivalent, as in: " ".join(cmd1) ==
> cmd2. But the first example returns a code 2, whereas the second runs
> successfully. What is the difference? I prefer using a list as it looks a
> little cleaner. Btw, shell=True is needed here.
>
>
> # Python 2.7.3 (default, Jan 2 2013, 13:56:14) [GCC 4.7.2] on linux2
>
> import subprocess
>
> #1# returns retcode 2 (whatever error that maybe)
> title, author, version = "title", "author", "1.0.0"
> output_dir, input_dir = '/tmp/input', '/tmp/output'
> cmd1 = [r'sphinx-apidoc',
> r'-f -F',
> r'-H', '"%s"' % title,
"title" becomes \"title\", i. e. Python puts in an extra effort to have the
quotes survive the subsequent parsing process of the shell:
>>> print subprocess.list2cmdline(['"title"'])
\"title\"
> r'-A', '"%s"' % author,
> r'-V', '"%s"' % version,
> r'-o', output_dir, input_dir]
> retcode = subprocess.call(cmd1, shell=True)
> assert not retcode, retcode
>
> #2# returns retcode 0 (succes)
> cmd2 = (r'sphinx-apidoc '
> r'-f -F '
> r'-H "%(title)s" '
> r'-A "%(author)s" '
> r'-V "%(version)s" '
> r'-o %(output_dir)s %(input_dir)s') % locals()
>
> retcode = subprocess.call(cmd2, shell=True)
> assert not retcode, retcode
>
> # no AssertionError
> assert " ".join(cmd1) == cmd2
>
>
>
> Thanks in advance!
>
> Regards,
>
> Albert-Jan
>
>
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> All right, but apart from the sanitation, the medicine, education, wine,
> public order, irrigation, roads, a
>
> fresh water system, and public health, what have the Romans ever done for
> us?
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list