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

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

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

Dec 4 '05 #1
Share this Question
Share on Google+
9 Replies


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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDkmXPJd01MZaTXX0RAiQtAJ0SpY7OyMzQr6PlkFI4Df lW1E63dgCdEy+g
1reF88jZY5/DQO86e6RTWbU=
=I5O4
-----END PGP SIGNATURE-----

Dec 4 '05 #2

P: n/a
I don't have any problems with spaces in the folders.

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

On 3 Dec 2005 19:16:10 -0800, sd*****@gmail.com <sd*****@gmail.com> wrote:
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

--
http://mail.python.org/mailman/listinfo/python-list

Dec 4 '05 #3

P: n/a
je****@unpythonic.net wrote:
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....40a65017848671


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

Dec 4 '05 #4

P: n/a
On 3 Dec 2005 19:16:10 -0800, "sd*****@gmail.com" <sd*****@gmail.com> wrote:
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
Dec 4 '05 #5

P: n/a
sd*****@gmail.com wrote:
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?

Dec 4 '05 #6

P: n/a
je****@unpythonic.net wrote:
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>.
Dec 4 '05 #7

P: n/a
Leif K-Brooks wrote:
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.
Dec 4 '05 #8

P: n/a
Peter Hansen wrote:
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:
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>

Dec 4 '05 #9

P: n/a
rbt
je****@unpythonic.net wrote:
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....40a65017848671


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))
Dec 6 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.