[python-win32] subprocess and stdout

Dave Angel davea at ieee.org
Mon Jul 27 12:09:38 EDT 2009


(This message was top-posted, and sent off-list.  So I'm copying it back 
to the list, with my response at the end)

Chris Chapman wrote:
> Thanks Dave.  You know after trying your suggestion on the command prompt it doesn't as a matter of fact.  Not sure why I didn't just try that in windows to begin with.  Guess I got stuck more on the python issue then it being the other program.
>
> There is an option for it to create an HTML log file, but it's formatted sort of strangely.  I'm really not looking to parse through it just for a summary like that presented on the terminal.  I'm not sure if you know but I'm guessing parsing the HTML would be a lot easier the trying to capture the console output from the buffer, huh?  Thanks again for the help.
>
> Chris
>
>   
>> Date: Mon, 27 Jul 2009 08:28:51 -0400
>> From: davea at ieee.org
>> To: oxydol010 at hotmail.com
>> CC: python-win32 at python.org
>> Subject: Re: [python-win32] subprocess and stdout
>>
>> Christopher Chapman wrote:
>>     
>>> I'm trying to write a "module" to a larger program.  This module eventually
>>> needs to reference a config file with setup information in it, but I'm not
>>> there yet.  I'm using the subprocess module to run an external antivirus
>>> program and trying to get the output written to a log file, but I keep
>>> getting a weird return.  My code is listed below.  I'm a noob to Python so
>>> I'm not sure if this is a windows specific issue or not, but I'm programming
>>> on windows and more then likely what I'm working on won't be on anything
>>> other then a windows machine.  I'm also writing my code for 3.1 if that
>>> makes any difference.
>>>
>>> # Pass target path to scanners command line and write output to a file
>>> # Currently "scan" only targets the McAfee Command Line Scanner
>>> print() 
>>> print ("Begining scan of " + target)
>>> print()
>>> scan = "scan /all " + target
>>> s = subprocess.Popen(scan,
>>>                      stdout=subprocess.PIPE)
>>> out = s.communicate()[0]
>>> chgout = str(out)
>>> s.wait()
>>> scanlog.write(chgout)
>>> scanlog.close
>>> "
>>> If I change my stdout in s subprocess to None then everything gets written
>>> to the terminal as if I had just run the program straight from the command
>>> line, but when I try piping it to my file "scanlog" then literally the only
>>> return I get in the file is '' or two single quotes.  I've even tried piping
>>> the output and then printing it instead of writing it to a file and I get
>>> the same result.  I've experimented with standard windows command line
>>> commands and using the same syntax was able to pipe directory listings and
>>> other things to my file.  Any ideas what I'm missing here?  Thanks in
>>> advance
>>>
>>> Chris Chapman
>>>
>>>
>>>   
>>>       
>> I suspect it's because of the way the antivirus program is written.  I 
>> can't be sure what you tried at the command prompt, but have you tried this:
>>
>> c:\>scan /all > scanlog.txt
>>
>>
>> If this does not capture to the file, then scan.exe isn't written in the 
>> way you're expecting.  There are several ways to write directly to a 
>> console that do not redirect or pipe.
>>
>> You might also look to see whether scan has any other commandline 
>> options.  One of them might be to create a log file.
>>
>> DaveA
>>     
>
>   

I wouldn't know where to begin trying to capture output that another 
process sends to its console window.  While I'm sure it's possible, I do 
suspect that parsing the html would be easier.  Remember that even if it 
looks ugly, it is generated by program, and it's quite possible that you 
could throw away the fluff and end up with a simple summary.  There are 
libraries to make this easier, but I've never used them.  If it's valid 
xhtml, which is by definition valid xml, you might find it useful to use 
elementree.  But most html is buggy, and parsing that takes a more 
tolerant parser.

DaveA




More information about the Python-list mailing list