How to execute an EXE via os.system() with spaces in the directory name?

S

sdb1031

I am trying to run an exe within a python script, but I'm having
trouble with spaces in the directory name.

The following example will display the usage statement of the program,
so I know that the space in the path to the exe is being handled
correctly and that the program was executed.

CMD= r'"C:\program files\some directory\engine\theexe.exe"'
os.system(CMD)

But the required argument for the exe require a path to a file to be
specified. When I try to run the following, os.system(CMD2)
CMD2= r'"C:\program files\some directory\engine\theexe.exe" "C:\program
files\some directory\engine\file.txt"'

I get this error:
Unable to open file C:\Program

So, it looks to me like the space in the path for the argument is
causing it to fail. Does anyone have any suggestions that could help
me out?
Thanks,
Steve
 
J

jepler

V

Vijay L

I don't have any problems with spaces in the folders.

just for debugging, you could probably try os.system(CMD.replace("\\", "/")
 
P

Peter Hansen

This comes up from time to time. The brain damage is all Windows', not
Python's. Here's one thread which seems to suggest a bizarre doubling
of the initial quote of the commandline.

http://groups.google.com/group/comp.lang.python/browse_frm/thread/89d94656ea393d5b/ef40a65017848671

It can't all be Windows' brain damage, since typing precisely the same
command at the prompt (at least with the example I'm using) doesn't
require doubling the initial quote of the command line. Or, more
precisely, Windows is brain damaged in at least two different places
here, and the shell is only one of them...

Also it appears the issue may be more the fact that the second argument
*also* has quotation marks (regardless of whether it has a space in it
or not). It's only then (it seems) that the silly double initial
quotation mark is required. Either way, "brain damage" definitely
describes it.

-Peter
 
B

Bengt Richter

I am trying to run an exe within a python script, but I'm having
trouble with spaces in the directory name.

The following example will display the usage statement of the program,
so I know that the space in the path to the exe is being handled
correctly and that the program was executed.

CMD= r'"C:\program files\some directory\engine\theexe.exe"'
os.system(CMD)

But the required argument for the exe require a path to a file to be
specified. When I try to run the following, os.system(CMD2)
CMD2= r'"C:\program files\some directory\engine\theexe.exe" "C:\program
files\some directory\engine\file.txt"'

I get this error:
Unable to open file C:\Program

So, it looks to me like the space in the path for the argument is
causing it to fail. Does anyone have any suggestions that could help
me out?
What version of windows and python are you running?
What happens if you leave out the space in the second quoted string
of CMD2? Does your program execute ok and see it?
What do you get for os.popen(CMD2).read() ?
What do you get if you make a cmd file like echoargs.cmd below
[23:29] C:\pywk\grammar>type c:\util\echoargs.cmd
@echo %*

and substitute echoargs (with path if necessary) in place of your executable in CMD2?

Just suggestions to get more symptoms for diagnosis.

Regards,
Bengt Richter
 
D

Dan Bishop

I am trying to run an exe within a python script, but I'm having
trouble with spaces in the directory name. ....
So, it looks to me like the space in the path for the argument is
causing it to fail. Does anyone have any suggestions that could help
me out?

Does C:\progra~1\somedi~1\engine\theexe.exe still work?
 
L

Leif K-Brooks

This comes up from time to time. The brain damage is all Windows',
not Python's.

It's perfectly reasonable behavior, and it also applies to Linux. The
shell uses spaces to separate arguments; how do you expect it to know
that you want a space to be part of the program's name unless you escape it?
Here's one thread which seems to suggest a bizarre doubling of the
initial quote of the commandline.

A better solution would be to use subprocess:
<http://python.org/doc/current/lib/module-subprocess.html>.
 
L

Leif K-Brooks

Leif said:
It's perfectly reasonable behavior, and it also applies to Linux. The
shell uses spaces to separate arguments; how do you expect it to know
that you want a space to be part of the program's name unless you escape it?

I'm sorry, disregard my message. I failed to read the OP properly.
 
F

Fredrik Lundh

Peter said:
It can't all be Windows' brain damage, since typing precisely the same
command at the prompt (at least with the example I'm using) doesn't
require doubling the initial quote of the command line. Or, more
precisely, Windows is brain damaged in at least two different places
here, and the shell is only one of them...

the system function in the C runtime library function simply runs
the following command:

%COMSPEC% /c command

where COMSPEC usually points to cmd.exe.

so in the normal case, it's up to cmd.exe to parse the command string.
it uses the following algorithm to decide if it should remove quotes from
the command string (for compatibility with command.com?), or if they're
better left in place:

...

1. If all of the following conditions are met, then quote characters
on the command line are preserved:

- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
the two quote characters
- the string between the two quote characters is the name
of an executable file.

2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.

...

</F>
 
R

rbt

This comes up from time to time. The brain damage is all Windows', not
Python's. Here's one thread which seems to suggest a bizarre doubling
of the initial quote of the commandline.

http://groups.google.com/group/comp.lang.python/browse_frm/thread/89d94656ea393d5b/ef40a65017848671

I do this:

# remove spaces from ends of filenames.
for root, dirs, files in os.walk('programs'):
for fname in files:
new_fname = fname.strip()
if new_fname != fname:
new_path = os.path.join(root,new_fname)
old_path = os.path.join(root,fname)
os.renames(old_path,new_path)

# remove spaces from middle of filenames.
for root, dirs, files in os.walk('programs'):
for f in files:
new_f = string.replace(f, ' ' , '-')
new_path = os.path.join(root,new_f)
old_path = os.path.join(root,f)
os.renames(old_path,new_path)

# install files.
for root, dirs, files in os.walk('programs'):
installable = ['.exe', '.msi', '.EXE', '.MSI']
for f in files:
ext = os.path.splitext(f)
if ext[1] in installable:
print f
install = os.system(os.path.join(root,f))
 

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,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top