473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
9 19018
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Manu | last post by:
Hi, ftplib fails to cwd into a directory which contains spaces in it's name.How do i correct this? Regards Manu
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
1
by: Trevor | last post by:
Also posted in general ASP.NET forum. System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. I have hit a problem for which I can find no solutions. Has...
1
by: nek | last post by:
Greetings, We try to automate database creation via script which gets executed remotely for a number of servers. This works fine for most of the servers. However, it occasionally fails some...
3
by: Ricardo | last post by:
How can I get the directory name that the aplication is being executed??? s...
3
by: Bryce | last post by:
I need to FTP a file to an FTP server -- the destination path has spaces. When I interactively change directory using FTP in a command window, I can put double quotes around a directory name that...
2
by: Nathan Sokalski | last post by:
I have an ASP.NET application which displays the directories & files in a specified directory on the server. I use the System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() to...
13
by: ogo796 | last post by:
hi everyone i upload file each an every day so i want to keep track of how many files i uploade a day by creating new directory everytime when i uploade base on the system date. example the new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.