[Tutor] os.popen() questions
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Jul 24 19:43:02 CEST 2004
Magnus Lyckå said unto the world upon 24/07/2004 06:42:
> At 19:19 2004-07-23 -0400, Brian van den Broek wrote:
>
>> 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
>
>
>
> Are you sure that clamscan.exe writes to stdout? Maybe it
> actually writes to stderr? I don't think popen.readlines
> will catch that (but DOS ">" will).
>
> You might want to use popen3 or popen4 instead.
>
> --
> Magnus Lycka (It's really Lyckå), magnus at thinkware.se
> Thinkware AB, Sweden, www.thinkware.se
> I code Python ~ The Agile Programming Language
Thanks Magnus (and Alan for a similar response),
that did it. The clamscan.exe program did indeed write to stderr rather
than stdout. (The 50+ pdf doc with the program said so -- I don't know if
I missed it, or, being new to this area, missed its significance :-[ )
I've a follow-up, if you don't mind.
I looked at Python in a Nutshell to see how to apply the advice I was
given. There I found a brief discussion of the difference between the
flavours of popen. Martelli says:
<q>
popen2 is simpler to use than popen3 when it's okay for cmd's standard
error to go to the same destination as your own process's standard error,
and popen4 is simpler when it's okay for cmd's standard error and output
to be mixed with each other.
</q>
What determines whether these things are "okay"? Is it highly program
dependant, or is there some generally applicable criteria for when this is
fine to do?
Thanks,
Brian vdB
More information about the Tutor
mailing list