By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,399 Members | 886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,399 IT Pros & Developers. It's quick & easy.

os.popen() not executing command on windows xp

P: n/a
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?

Jan 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
At Thursday 11/1/2007 06:42, nic wrote:
>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

Jan 11 '07 #2

P: n/a
import os
a = os.popen('"c:\Program Files\Grisoft\AVG Free\avgscan.exe"
"c:\program files\temp1\test1.txt"')
print a.read()
use raw strings

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

http://www.ferg.org/projects/python_...ontents_item_2

Jan 11 '07 #3

P: n/a
nic
Justin Ezequiel wrote:
import os
a = os.popen('"c:\Program Files\Grisoft\AVG Free\avgscan.exe"
"c:\program files\temp1\test1.txt"')
print a.read()

use raw strings

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

http://www.ferg.org/projects/python_...ontents_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.

Jan 11 '07 #4

P: n/a
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.

Jan 11 '07 #5

P: n/a
At Thursday 11/1/2007 14:09, nic wrote:
>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

Jan 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.