[Tutor] importing a subprocess in dos

Kirby Urner urnerk@qwest.net
Fri, 29 Mar 2002 15:06:43 -0800


At 05:44 PM 3/29/2002 +0000, Bruce Gollng wrote:
>Hello all,
>I'm missing this part in the mans, so some help would be appreciated.  How
>do I run a process in dos (btw my os is win98) and import the result to my
>python interpreter?

Here's one way (I'm using shell mode):

  >>> import os
  >>> def dodos(cmd):
         f = os.popen(cmd)
         return f.readlines()

  >>> mydir = dodos('dir')

  >>> for i in mydir: print i,


  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
  TOOLS          <DIR>        07-22-01 10:40p Tools
  DOC            <DIR>        07-22-01 10:40p Doc
  TCL            <DIR>        07-22-01 10:40p tcl
  WORK           <DIR>        08-29-01  1:33a work

etc.

Kirby