Mailing List Archive

subprocess module: execution of standard binaries without shell?
hi all

while getting used to with subprocess module i failed in executuing a)
but succeeded in running b). Can anyone explain me why as i am providing
absolute path? Is this has to do anything with shared library.. which
must be accessed based on system variables?


a) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
close_fds=True)
==>OSError: [Errno 2] No such file or directory

b) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
close_fds=True, shell=True)

--
Thanks & Regards
visco

--
http://mail.python.org/mailman/listinfo/python-list
Re: subprocess module: execution of standard binaries without shell? [ In reply to ]
Visco Shaun schrieb:
> hi all
>
> while getting used to with subprocess module i failed in executuing a)
> but succeeded in running b). Can anyone explain me why as i am providing
> absolute path? Is this has to do anything with shared library.. which
> must be accessed based on system variables?
>
>
> a) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
> close_fds=True)
> ==>OSError: [Errno 2] No such file or directory

You have to use a list instead of a string here.

pipe = subprocess.Popen(["/bin/ls", "/"], stdout=subprocess.PIPE)

--
http://mail.python.org/mailman/listinfo/python-list
Re: subprocess module: execution of standard binaries without shell? [ In reply to ]
On Thu, Feb 26, 2009 at 2:41 AM, Visco Shaun <visco31@gmail.com> wrote:
> hi all
>
> while getting used to with subprocess module i failed in executuing a)
> but succeeded in running b). Can anyone explain me why as i am providing
> absolute path? Is this has to do anything with shared library.. which
> must be accessed based on system variables?
>
>
> a) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
> close_fds=True)
>                ==>OSError: [Errno 2] No such file or directory

You need to use a list of arguments, not just a string. You're
currently telling Python to try and run a nonexistent directory
(specifically, the "ls " subdirectory of /bin), since the string way
of calling Popen assumes that the *entire* string is the path to the
executable when shell=False.

The correct way is to provide the path to the binary and then each of
its arguments, in a list:
pipe = subprocess.Popen(["/bin/ls", "/"], stdout=subprocess.PIPE,
close_fds=True)

> b) pipe = subprocess.Popen("/bin/ls /", stdout=subprocess.PIPE,
> close_fds=True, shell=True)

This works because shell=True sends the string through the shell,
which tokenizes it and runs it, effectively splitting the string into
a list for you. However, shell=True is dangerous as you need to be
careful to escape special characters, whereas that's not necessary for
the 'shell=False and list' way of calling Popen.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list