output of shell command

D-Man dsh8290 at rit.edu
Tue Feb 27 14:16:53 EST 2001


On Mon, Feb 26, 2001 at 04:38:58AM -0800, Emile van Sebille wrote:
| However, a=os.popen("dir").readlines() works on windows and
| linux.  So if you're writing cross-platform, you'll want to
| keep this in mind.
| 

This assumes the command exists in the PATH on both systems and works
the same way.  Normally "ls" doesn't exist on Windows.  It's possible
that "dir" exists on Linux, but I've never tried it.  Other *nix's may
or may not have a "dir" command.

For this particular case, listing a directory, it would be better to
use :

import os
import string
dir_list = os.listdir( os.getcwd() )
print string.join( dir_list , "\n" )


As it will work on all platforms regardless of the
existence/name/semantics of external commands.

-D





More information about the Python-list mailing list