[Tutor] subprocess.call list vs. str argument

Dave Angel davea at davea.name
Tue Feb 25 21:41:44 CET 2014


 Albert-Jan Roskam <fomcl at yahoo.com> Wrote in message:
> 
> 
> ----- Original Message -----
> 
>> From: Danny Yoo <dyoo at hashcollision.org>
>> To: Peter Otten <__peter__ at web.de>
>> Cc: Python Tutor Mailing List <tutor at python.org>
>> Sent: Monday, February 24, 2014 11:01 PM
>> Subject: Re: [Tutor] subprocess.call list vs. str argument
>> 
>> Last comment (apologies for being so fragmented!).  I don't know why
>> the literal strings there are raw there.  Altogether, I'd expect:
>> 
>> #############################################
>> cmd1 = ['sphinx-apidoc',
>>        '-f',
>>        '-F',
>>        '-H', title,
>>        '-A', author,
>>        '-V', version,
>>        '-o', output_dir,
>>        input_dir]
>> retcode = subprocess.call(cmd1, shell=False)
>> 
>> #############################################
> 
> 
> Hi Danny, Peter,
> 
> Thanks for helping me. Danny, your example works indeed and it is also very readable. And now I do not need to use shell=True anymore. The raw strings were a leftover from earlier attempts that contained the entire path (not even relevant under Linux, but I still did it). Here is why I used "shell=True" before. Is it related to the file paths?
> 
>     cmd = (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(cmd)
> Traceback (most recent call last):
>   File "/home/albertjan/Downloads/documenter.py", line 188, in <module>
>     rst_to_html(input_dir, output_dir, title="Test", author=getpass.getuser(), version="1.0.0")
>   File "/home/albertjan/Downloads/documenter.py", line 118, in rst_to_html
>     retcode = subprocess.call(cmd)
>   File "/usr/lib/python2.7/subprocess.py", line 493, in call
>     return Popen(*popenargs, **kwargs).wait()
>   File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
>     errread, errwrite)
>   File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
>     raise child_exception
> OSError: [Errno 2] No such file or directory
> 

Here you are feeding a string to the call function,  while Danny
 was using a list.  With a string,  you must use shell=True, so
 the shell can turn it into a list. Usually much easier to use a
 list in the first place. 

-- 
DaveA



More information about the Tutor mailing list