without shell

David M. Cooke cookedm+news at physics.mcmaster.ca
Fri Jun 10 19:59:39 CEST 2005


Donn Cave <donn at u.washington.edu> writes:

> In article <11aj9d3fga9or8b at corp.supernews.com>,
>  Grant Edwards <grante at visi.com> wrote:
>
>> On 2005-06-10, Mage <mage at mage.hu> wrote:
>> 
>> >>py> file_list = os.popen("ls").read()
>> >>
>> >>Stores the output of ls into file_list.
>> >>
>> > These commands invoke shell indeed.
>> 
>> Under Unix, popen will not invoke a shell if it's passed a
>> sequence rather than a single string.
>
> I suspect you're thinking of the popen2 functions.
> On UNIX, os.popen is posix.popen, is a simple wrapper
> around the C library popen.  It always invokes the
> shell.
>
> The no-shell alternatives are spawnv (instead of
> system) and the popen2 family (given a sequence
> of strings.)

Don't forget the one module to rule them all, subprocess:

file_list = subprocess.Popen(['ls'], stdout=subprocess.PIPE).communicate()[0]

which by default won't use the shell (unless you pass shell=True to it).

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list