os.popen() not executing command on windows xp

N

nic

On my system (WinXP) typing the following line into command
prompt(cmd.exe) successfully scans the file test1.txt:
"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt"

Yet the python script:
import os
a = os.popen('"c:\Program Files\Grisoft\AVG Free\avgscan.exe"
"c:\program files\temp1\test1.txt"')
print a.read()

Returns a blank line, and I doesn't scan the file. (Note I've used
os.popen() successfully on my system in other situations like:
os.popen('taskkill /F /IM taskname.exe')).

I have a feeling my avgscan example shown above not working has
something to do with calling a 3rd party software program (avgscan.exe)
through popen, but I don't know why this won't work, when manually
typing it in the command line does?
 
G

Gabriel Genellina

a = os.popen('"c:\Program Files\Grisoft\AVG Free\avgscan.exe"
"c:\program files\temp1\test1.txt"')

Your string contains backquotes, and they have to be escaped.
Either use a raw string: os.popen(r'"c:\Program...) or double all
backquotes: os.popen('"c:\\Program...)


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 
N

nic

Justin said:
use raw strings

e.g., instead of '"c:...\avgscan...'
use r'"c:...\avgscan...'

http://www.ferg.org/projects/python_gotchas.html#contents_item_2

Sorry I initally had retyped the code to replace some variables to make
it more clear, I already had them as raw strings:

import os
pstr = r'"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt"'
a = os.popen(pstr)
print a.read()

I've confirmed the string I'm inputting to os.popen is EXACTLY the same
as the one I can successfully execute manually in command prompt, so
when I go:
print pstr, it yields:
"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt"

The problem remains popen won't execute this line as it does when
inputted manually to command prompt.
 
N

nic

It appears os.popen() doesn't like full windows paths with spaces (e.g.
"c:/program files") so I'm going to use the subprocess module.
 
G

Gabriel Genellina

import os
pstr = r'"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt"'
a = os.popen(pstr)
print a.read()

I've confirmed the string I'm inputting to os.popen is EXACTLY the same
as the one I can successfully execute manually in command prompt, so
when I go:
print pstr, it yields:
"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt"

The problem remains popen won't execute this line as it does when
inputted manually to command prompt.

I have AVG so I tried this example. That's not the problem. popen
works ok, avgscan is executed. But the avgscan program does *not* use
stdout nor stderr to write to the console, so popen can't capture its output.
You can confirm this at the command prompt:

"c:\Program Files\Grisoft\AVG Free\avgscan.exe" "c:\program
files\temp1\test1.txt" >output.txt 2>&1

You will see the usual progress messages from avgscan *on screen*,
and when you type output.txt, it's empty. avgscan appears to write
directly to the console.
You may use the /REPORT option; avgscan /? for details.


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top