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

Bengt Richter bokr at oz.net
Sun Dec 4 02:33:06 EST 2005


On 3 Dec 2005 19:16:10 -0800, "sdb1031 at gmail.com" <sdb1031 at 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



More information about the Python-list mailing list