output from external commands

darren kirby bulliver at badcomputer.org
Mon Oct 24 09:53:38 CEST 2005

quoth the James Colannino:
> Hey everyone.  First off, I'm new to the list.  I had had a little bit
> of experience with Perl before discovering Python.  The more Python I
> learn, the more I love it :)  I just have a quick question to ask.  I
> know that this is probably a simple question, but I've been googling
> around, and partly because I'm not sure exactly what to search for, I've
> been unsuccessful at finding an answer.  What I'd like to do is be able
> to take the output of an external command and assign it as an array of
> strings.  So, for example, in Perl I could do something like:
> @files = `ls`;
> So I guess I'm looking for something similiar to the backticks in Perl.
> Forgive me if I've asked something that's a bit basic for this list.
> Any help would be greatly appreciated :)  Thanks very much in advance.

If all you want is filenames this will work:
>>> import glob
>>> files = ["%s" % f for f in glob.glob("*")]

Else use os.popen to iterate over lines of output:
>>> import os
>>> for line in os.popen("ls -l").readlines():
>>> . . . process(line)

Or check out subprocess if you have 2.4..

> James
> --
> My blog: http://www.crazydrclaw.com/
> My homepage: http://james.colannino.org/

darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20051024/677c8f1f/attachment.pgp>

More information about the Python-list mailing list