Hi All,

I need to open a exe from python. For ex i used

import os
os.system("C:\Winamp\Winamp.exe")

but I need to open an exe from the "Program Files" directory. I get an error because of the space between "Program" and "Files".

import os
os.system("C:\Program Files\Winamp\Winamp.exe")
>>'C:\Program' is not recognized as an internal or external command.

Anybody has the solution. It must be simple but I am not able to get it :(

Hi All,

import os
os.system("C:\Program Files\Winamp\Winamp.exe")
>>'C:\Program' is not recognized as an internal or external command.

Anybody has the solution. It must be simple but I am not able to get it :(

Shouldn't you be doing it like

import os
os.system("C:\\Program\ Files\\Winamp\\Winamp.exe")

?

I tried

os.system("C:\\Program\ Files\\Winamp\\Winamp.exe")
os.system("C:\\Program/ Files\\Winamp\\Winamp.exe")

I get the same error

Member Avatar for leegeorg07

try:

temp_var=r("C:\Program files\Winamp\Winamp.exe")
os.system(tempvar)

Im not sure if defining it as a raw variable would work but its worth a shot

Maybe

os.system("\"C:\Program Files\Winamp\Winamp.exe\"")

might be silly, but who knows...

Thanks leegeorg and ryuslash.. I tried both ways but i still get the same error. :'(

It's not possible that you don't actually have Winamp in Program Files? You said that you tested with C:\Winamp\Winamp.exe which worked?

It's not possible that you don't actually have Winamp in Program Files? You said that you tested with C:\Winamp\Winamp.exe which worked?

Actually I just gave that as an example..

If it were not present then the error would be "The System cannot find the path specified"

but I get an error 'C:\Program' is not recognized as an internal or external command.

Maybe i should reinstall my program out of program files folder but i want to do it as a last resort

Thought so

It took a bit of playing around, but think I've found a workaround for this....

I noticed in the docs for os.system() (Yes I RTFM!) it mentions that the subprocess.call() should be used instead.
So I had a go with subprocess.call() but I soon ran into very similar problems, it still didn't like the spaces in the path.

After a bit of playing around and using a bit of lateral thinking I decided to try using subprocess.call() to call 'cmd.exe' passing it the path to a program in 'Program Files' as a parameter.

Now, I don't have winamp installed on my pc, so I decided to try and fire up Audacity using the following code:

import subprocess

subprocess.call(r'C:\WINDOWS\system32\cmd.exe /C "C:\Program Files\Audacity\audacity.exe"')

And you know what?? It bloody works!

After that, I decided to try using os.system in exactly the same way (use it to fire up cmd.exe with the path to Audacity as a parameter):

import os

os.system(r'C:\WINDOWS\system32\cmd.exe /C "C:\Program Files\Audacity\audacity.exe"')

Guess what??
That worked too!

Woot woot! :)

You'll get a little cmd window that will pop-up alongside whatever program you fire up, but it will disappear when you close the program down again.

So, all you need to do is copy one of the bits of code posted above and then replace my path to Audacity with the path to your installation of Winamp!

Hope that solves your problem.
Cheers for now,
Jas.

commented: cool.. Thanks +1

I've just done a bit of digging around on python.org and found this:
http://bugs.python.org/issue1524

Looks like I'm not the first person to come up with this workaround....See the post made by daniel.weyh on the 25th June 2008.

Jas.

Hope that solves your problem.

Bingo!!! It sure did.. Thanks a lot Jason :)

I think my googling skills need improvement :)

This problem has been around for a long time and not just with Python:

# the "Microsoft kludge", quoting a string within a string fixes the 
# space-in-folder-name problem, tells the OS to use the whole string
# including spaces as a single command
# (make sure filename does not contain any spaces!)
os.system('"C:/Program Files/IrfanView/i_view32.exe" ' + filename)
commented: Nicely done....How did I miss that?! +4
commented: nice trick +5
commented: Perfect +1
os.system('"C:/Program Files/IrfanView/i_view32.exe" ' + filename)

Dammit...I thought I'd tried that combination of string in string earlier..... Obviously that was one permutation I missed!

Hang on....Looking back at my failed scripts from when I was playing around, I had the " and the ' the wrong way around! Damn these fat drummer fingers! grrr! heh heh! :D

Thanks for that Ene! Yes that's far better than my solution!

Cheers for now,
Jas.

This is often referred to as the little-known "Microsoft kludge"

Member Avatar for leegeorg07

just thinking... wouldnt it have made more sense to just change "Program files" to "Program_files" or something similar?

just thinking... wouldnt it have made more sense to just change "Program files" to "Program_files" or something similar?

Not really, because in MS Windows the 'Program Files' folder is the system default folder for programs to be installed in (and there are usually a lot of programs installed there!).

Simply renaming the 'Program Files' folder to 'Program_Files' would cause most (if not all) of your programs to either malfunction, or not work at all when you tried to run them.
The OS probably wouldn't take it too well either!

Plus any shortcuts you have for any programs in the 'Program Files' folder will also stop working...(e.g. desktop shortcuts and start menu links)

So in this case...No! Changing the folder name would definitely not be the best option. Ene's line of code is by far the best solution to the problem!

Cheers for now,
Jas. :P

Something only Microsoft could dream up. I understand that in the European version of Windows the space is avoided. The kludge itself is from a programmer within Microsoft.

Member Avatar for leegeorg07

nope, in the eu version it still has the space... at least for me :(

Its actually not as tough as people are making it out to be, if you have a space you need to put ' ' or " " around it to clarify that it has a space, maybe try this:

import os
os.system("'C:\Winamp\Winamp.exe'")

I may be wrong, i might have just been using Linux too long :P

Oh an another thing to try is CD to the program files, then call os.system

os.chdir("C:/Program Files/Winamp")
os.system("Winamp.exe")

Oh an another thing to try is CD to the program files, then call os.system

os.chdir("C:/Program Files/Winamp")
os.system("Winamp.exe")
#
os.system('"C:/Program Files/IrfanView/i_view32.exe" ' + filename)

Thank you :)

Thank you :)

No problem. So case closed? Mark one down as solve ;P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.