A problem with subprocess

C

Colin J. Williams

Below is a test script:

# tSubProcess.py

import subprocess
import sys
try:
v= subprocess.Popen('ftype
py=C:\Python25\Python.exe')
except WindowsError:
print(sys.exc_info())

Here is the output:

*** Python 2.5.4 (r254:67916, Dec 23
2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32. ***Command Line : --Python25
(<type 'exceptions.WindowsError'>,
WindowsError(2, 'The system cannot find
the file specified'), <traceback object
at 0x021864E0>)
The doc seems to indicate that the
command can be in "args", in this case
"executable" is unnecessary.

Am I misreading the doc?

Colin W.
 
A

Albert Hopkins

Below is a test script:

# tSubProcess.py

import subprocess
import sys
try:
v= subprocess.Popen('ftype
py=C:\Python25\Python.exe')
except WindowsError:
print(sys.exc_info())

Here is the output:

*** Python 2.5.4 (r254:67916, Dec 23
2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32. ***Command Line : --Python25
(<type 'exceptions.WindowsError'>,
WindowsError(2, 'The system cannot find
the file specified'), <traceback object
at 0x021864E0>)
The doc seems to indicate that the
command can be in "args", in this case
"executable" is unnecessary.

Am I misreading the doc?

Not that I'm a Windows user, but your example doesn't appear to make any
sense to me.

According to the docs:

args should be a string, or a sequence of program arguments. The
program to execute is normally the first item in the args sequence
or
string, but can be explicitly set by using the executable argument.

There is no executable argument passed in your example, so the first
argument should point to an executable.

'ftype py=C:\Python25\Python.exe' doesn't seem like an executable to me.

-a
 
T

Tim Golden

Colin said:
Below is a test script:

# tSubProcess.py

import subprocess
import sys
try:
v= subprocess.Popen('ftype py=C:\Python25\Python.exe')
except WindowsError:
print(sys.exc_info())

I'm assuming that you've previously done something like this:

assoc .py=py

and are now trying to do this:

ftype py=c:\python25\python.exe

You've got a few issues to resolve there. If you're using a
string containing a backslash (basically, any Windows path)
you need to tell Python to treat the backslash as a backslash
and not as an escape character. Simplest way to do this is
to prefix the string with r for raw:

r"ftype py=c:\python25\python.exe"

In this case it wouldn't matter too much because \p isn't
a recognised escape sequence. But as soon as you hit, say,
"c:\files" you'll be in trouble because \f is the escape
sequence for ASCII 12.


OK. The next thing is that ftype is not actually an .exe
in its own right: it's provided by the command shell
(cmd.exe or whatever). That means you have to ask the
subprocess module to call the shell on your behalf:

subprocess.Popen (r"...", shell=True)

Finally, for simple do-it-and-finish subprocess calls like
yours, it's generally advantageous to use the convenience
function .call, and pass the params as a list of params
which avoid other issues to do with embedded spaces.
(Usually: there is an outstanding bug there). So...


import subprocess
subprocess.call (["ftype", r"py=c:\python25\python.exe"], shell=True)


TJG
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top