Using doctest and executing a process

Cliff Wells logiplexsoftware at earthlink.net
Mon Mar 11 17:48:19 EST 2002


On 11 Mar 2002 14:49:55 -0800
Douglas Garstang wrote:

> def tester3():
>     """
>     >>> import os
>     >>> os.system ('/bin/ls')
>     example.py   example.pyc  tester.py
>     0
>     """

[snip]

> WHY? It runs fine interactively. It also knows to expect the right
> stuff. It just SEEMS to think its only getting the return status code
> of 0. Where did the other output from the external program go? I also
> tried using os.spawnv instead of os.system and I got the same results.

os.system returns the exit status of the program (0 in this case).  The
directory listing went to stdout.  Try:

import os
ls = os.popen("/bin/ls", 'r').read()

which will capture stdout into the variable ls.



-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308




More information about the Python-list mailing list