[python-win32] subprocess and stdout

Dave Angel davea at ieee.org
Mon Jul 27 14:28:51 CEST 2009


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


More information about the python-win32 mailing list