[Tutor] routine command

Kirby Urner urnerk@qwest.net
Wed, 03 Apr 2002 15:21:30 -0800


At 08:46 PM 4/3/2002 +0200, you wrote:



>Greetings,
>                      I am looking for a way to
>call programs or directories from the Python
>GUI or otherwise. I am using WinME and
>Win2000. Ideas anyone?
>tanks gil
>

See os.system(command) or os.popen(command).

In theory, you can go os.system('notepad') in WinMe and
get notepad, but I always get a warning that this program
runs in real mode, which this version of windows doesn't
support.

But os.popen('notepad') works.  If you expect your command
to generate text in a shell, then you can write something
like:

def doscmd(cmd):
         f = popen(cmd)
         for i in f.readlines(): print i,
         f.close()

to echo it in Python. E.g.:

 >>> doscmd('dir')

  Volume in drive D is CAROL
  Volume Serial Number is 38D6-68D3
  Directory of D:\Program Files\Python22

.              <DIR>        07-22-01 10:40p .
..             <DIR>        07-22-01 10:40p ..
DLLS           <DIR>        07-22-01 10:40p DLLs
LIBS           <DIR>        07-22-01 10:40p libs
LIB            <DIR>        07-22-01 10:40p Lib
INCLUDE        <DIR>        07-22-01 10:40p include

etc. etc.

Or if you don't want to print it, but parse it, you
can return the lines in a list.

def doscmd(cmd):
         f = popen(cmd)
         output = f.readlines()
        f.close() # optional
         return output

Kirby