954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

opening an exe from Program Files directory in windows

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 :(

pysup
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 3
 

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")


?

ryuslash
Junior Poster in Training
57 posts since Jul 2009
Reputation Points: 32
Solved Threads: 13
 

I tried

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


I get the same error

pysup
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 3
 

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

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

Maybe

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

might be silly, but who knows...

ryuslash
Junior Poster in Training
57 posts since Jul 2009
Reputation Points: 32
Solved Threads: 13
 

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

pysup
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 3
 

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?

ryuslash
Junior Poster in Training
57 posts since Jul 2009
Reputation Points: 32
Solved Threads: 13
 
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

pysup
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 3
 

Thought so

ryuslash
Junior Poster in Training
57 posts since Jul 2009
Reputation Points: 32
Solved Threads: 13
 

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 
Hope that solves your problem.

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

I think my googling skills need improvement :)

pysup
Newbie Poster
18 posts since Sep 2009
Reputation Points: 10
Solved Threads: 3
 

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)
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 
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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 
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

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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.

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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

leegeorg07
Posting Pro in Training
428 posts since Jul 2008
Reputation Points: 35
Solved Threads: 32
 

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

ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You