[Tutor] running a .exe

Kent Johnson kent37 at tds.net
Fri Dec 23 01:46:57 CET 2005


Robin Buyer wrote:
> I created a small program to test os.system:
> 
> import os.path
> os.system("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
> 
> when i run this from the command line I get an error message:
> 'C:\Program' is not recognized as an internal or external command, operable
> program or batch file.
> 
> How do you put spaces into a path name?

The same way you do if you are typing the command directly to the shell - put it in 
quotes. So now there are two sets of quotes - one to tell Python it is a string, and one 
to pass to the shell:
os.system('"C:\Program Files\Internet Explorer\IEXPLORE.EXE"')

Alternately use subprocess.call() which takes a list of command-line parameters so it 
knows to quote the first arg:
  >>> import subprocess
  >>> subprocess.call([r'C:\Program Files\Internet Explorer\IEXPLORE.EXE'])

Also note that if you want to use paths with \ in them in Python strings you should use a 
raw string, otherwise the \ start escape sequences that you don't intend:
os.system(r'"C:\Program Files\Internet Explorer\IEXPLORE.EXE"')

Kent

PS please respond to the list.



More information about the Tutor mailing list