Discussion:
How to execute an EXE via os.system() with spaces in the directory name?
(too old to reply)
s***@gmail.com
2005-12-04 03:16:10 UTC
Permalink
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***@unpythonic.net
2005-12-04 03:43:11 UTC
Permalink
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
Leif K-Brooks
2005-12-04 09:43:57 UTC
Permalink
Post by j***@unpythonic.net
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?
Post by j***@unpythonic.net
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>.
Leif K-Brooks
2005-12-04 09:47:35 UTC
Permalink
Post by Leif K-Brooks
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.
rbt
2005-12-06 13:20:27 UTC
Permalink
Post by j***@unpythonic.net
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))

Vijay L
2005-12-04 03:52:19 UTC
Permalink
I don't have any problems with spaces in the folders.

just for debugging, you could probably try os.system(CMD.replace("\\", "/")
Post by s***@gmail.com
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"'
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
--
http://mail.python.org/mailman/listinfo/python-list
Peter Hansen
2005-12-04 04:31:52 UTC
Permalink
Post by j***@unpythonic.net
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
Bengt Richter
2005-12-04 07:33:06 UTC
Permalink
Post by s***@gmail.com
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"'
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
Dan Bishop
2005-12-04 08:16:59 UTC
Permalink
Post by s***@gmail.com
I am trying to run an exe within a python script, but I'm having
trouble with spaces in the directory name.
...
Post by s***@gmail.com
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?
Fredrik Lundh
2005-12-04 11:13:01 UTC
Permalink
Post by Peter Hansen
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
Post by Peter Hansen
cmd /?
...

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>
Loading...