Command line

Bengt Richter bokr at oz.net
Mon Nov 21 18:31:39 EST 2005


On 20 Nov 2005 12:00:02 -0800, "amfr" <amfr.org at gmail.com> wrote:

>Hoe would I call something on the command line from python, e.g. "ls
>-la"?
>
Depends on how much control you want over where the output goes.
If you want the result as a multi-line string formatted the way
the system utility (ls here) formats it, and that you can process
further (or print etc.) in your python script or interactive session,
you may want

 >>> import os
 >>> result = os.popen('ls -la').read()
 >>> print result
 Usage: LS [/FrqRdlt1sSvu] [files]

Oops, the ls I have on NT doesn't support "a" ;-)
But you get the idea.

You might have found this via the second link in Fredrik's post
( http://docs.python.org/lib/os-newstreams.html#os-newstreams )
but I thought you might miss it ;-)

If you don't want the original system utility formatting, the
python functions Fredrik showed make more sense, and are easier
to use.

( posting delayed >12 hrs due to news server prob ;-/ )

Regards,
Bengt Richter



More information about the Python-list mailing list