[Tutor] os.popen() questions (original) (raw)

Brian van den Broek bvande at po-box.mcgill.ca
Sat Jul 24 01:19:11 CEST 2004


Hi all,

I'm working through early chapter of Lutz's Programming Python and I am trying to learn how to use os.system() and os.popen(). At an early stage I am puzzled. :-(

I've a virus scanner that was written for Unix and ported to Windows via Cygwin (about which I know little). I can run the program fine from a DOS command line. What I am wanting to do is write a python script that will run it as though it were typed on a command line and give the data back to the python script. Ultimately, I want to do this for every dir in my tree, using os.chdir before each invocation.

I'm trying to use os.popen to do this, but obviously there is something I'm not getting. (This area is new to me, so it is entirely possible I am using the wrong tool.) I wrote a short program to try it and another os.popen() call to see that I could use it successfully in some cases. I'm using Python 2.3.4 and Windows Me.

import os the_program = r'C:\clamav-devel\bin\clamscan.exe' os.chdir('C:/testdir') a = os.popen('dir .').readlines() # works fine os.system('start %s > testing.txt' %the_program) b = os.popen(the_program).readlines() print b # prints as [] print a

Both a = os.popen('dir .').readlines() and os.system('start %s > testing.txt' %the_program) work as expected. The first gives me a list of lines just like the output from typing the command in a DOS box and the second produces a txt file with what is normally the screen output of running the target program in a DOS console. So, why does b = os.popen(the_program).readlines() make b an empty list (as per last line of the output)? Am I misunderstanding what is going on here? Nothing in the docs or Programming Python has shed light on this for me.

Could it be some peculiarity of the program that I am running within the os.popen() call that is causing this? I have noticed that the file testing.txt has UNIX style EOL. But I also tried using b = os.popen(the_program, 'rb').readlines() and had the same results (b == []).

Thanks for any pointers on these issues. Best,

Brian vdB



More information about the Tutor mailing list