views:

18530

answers:

6

I have a python script that needs to execute an external program, but for some reason fails.

If I have the following script:

import os;
os.system("C:\\Temp\\a b c\\Notepad.exe");
raw_input();

Then it fails with the following error:

'C:\Temp\a' is not recognized as an internal or external command, operable program or batch file.

If I escape the program with quotes:

import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe"');
raw_input();

Then it works. However, if I add a parameter, it stops working again:

import os;
os.system('"C:\\Temp\\a b c\\Notepad.exe" "C:\\test.txt"');
raw_input();

What is the right way to execute a program and wait for it to complete? I do not need to read output from it, as it is a visual program that does a job and then just exits, but I need to wait for it to complete.

Also note, moving the program to a non-spaced path is not an option either.


Edit This does not work either:

import os;
os.system("'C:\\Temp\\a b c\\Notepad.exe'");
raw_input();

Note the swapped single/double quotes.

with or without a parameter to notepad here, it fails with the error message

The filename, directory name, or volume label syntax is incorrect.
A: 

I suspect it's the same problem as when you use shortcuts in Windows... Try this:

import os;
os.system("\"C:\\Temp\\a b c\\Notepad.exe\" C:\\test.txt");
Matthew Scharley
sorry, that does not work either, edited question to reflect this.
Lasse V. Karlsen
I think windows only uses ", rather than ' for quoting. This will probably work if you change this. However you'll still run into problems with if you have embedded quotes etc.
Brian
I thought it took both, but you're probably right. I know it works (in the shell atleast) with double quotes.
Matthew Scharley
+21  A: 

Subprocess.call will avoid problems with having to deal with quoting conventions of various shells. It accepts a list, rather than a string, so arguments are more easily delimited. ie

import subprocess
subprocess.call(['C:\\Temp\\a b c\\Notepad.exe'])
Brian
Thanks, that worked nicely for all the cases.
Lasse V. Karlsen
It is much simpler to use raw string in windows: r"C:\Temp\a b c\Notepad.exe"
PierreBdR
Another option may or may not be to use the exec* functions which are also in sys from memory. They take an array in a similar fashion, but I don't think they ever return. Not sure about the semantics here, I recall it mentioned overwriting the current process though, so...
Matthew Scharley
Yes, the os.exec* functions will replace the current process, so your python process won't continue. They're used more on unix where the general method for a shell to launch a command is to fork() and then exec() in the child.
Brian
The windows method for this is the os.spawn family, which could be used instead. subprocess is more portable though, and offers more flexibility in controlling the process (capturing input/output etc), so is preferred.
Brian
@PierreBdr: There is a case where rawstrings won't work: where you need a trailing slash. eg r'c:\foo\bar\'. Actually, its probably better to use forward slashes instead. These are accepted throughout the windows API (though not always by some shell commands (eg copy))
Brian
+1  A: 

Here's a different way of doing it.

If you're using windows the following acts like double-clicking the file in Explorer, or giving the file name as an argument to the DOS "start" command: the file is opened with whatever application (if any) its extension is associated.

import os
os.startfile(filepath)

Example:

import os
os.startfile(textfile.txt)

This will open textfile.txt with notepad if notepad is associted with .txt files.

+4  A: 

The outermost quotes are consumed by Python itself, and the Windows shell doesn't see it. As mentioned above, Windows only understands double-quotes. Python will convert forward-slashed to backslashes on Windows, so you can use

os.system('"C:/Temp/a b c/Notepad.exe"')

The ' is consumed by Python, which then passes "C:\Temp\a b c\Notepad.exe" (as a Windows path, no double-backslashes needed) to CMD.EXE

Daniel Serodio
+1  A: 

At least in Windows 7 and Python 3.1, os.system in Windows wants the command line double-quoted if there are spaces in path to the command. For example:

  TheCommand = '\"\"C:\\Temp\\a b c\\Notepad.exe\"\"'
  os.system(TheCommand)

A real-world example that was stumping me was cloning a drive in Virtual box. The subprocess.call solution above didn't work because of some access rights issue, but when I double-quoted the command, os.system became happy:

  TheCommand = '\"\"C:\\Program Files\\Sun\\VirtualBox\\VBoxManage.exe\" ' \
                 + ' clonehd \"' + OrigFile + '\" \"' + NewFile + '\"\"'
  os.system(TheCommand)
Paul Hoffman
A: 

import win32api # if active state python is installed or install pywin32 package seperately

try: win32api.WinExec('NOTEPAD.exe') # Works seamlessly except: pass

rahul