This is a classic problem with the command shell. It isn't really a Windows specific problem, except that on *nix, people never really got into the habit of putting spaces in file names, and Windows puts spaces in several default system places such as C:\Program Files
.
What is happening is that os.execute(str)
is implemented in terms of the ANSI C function system(str)
, which on Windows tries to duplicate the effect of typing "cmd /C "..str
to the command prompt. (On *nix, it uses /bin/sh -c instead of cmd /C.)
The classic problem is that this has to split the complete command string at whitespace to decide what program to run, and what its arguments are.
Your original example: os.execute("C:\\Program Files\\Movie Maker\\moviemk.exe")
effectively became cmd /c c:\program files\movie maker\moviemk.exe
, which after splitting it up on whitespace, CMD tried to find a program named c:\program
to execute with arguments named files\movie
and maker\moviemk.exe
. This isn't what you intended.
The solution is to be much more defensive about quoting.
I would write this as:
os.execute [["C:\Program Files\Movie Maker\Moviemk.exe"]]
If there were additional command line arguments to be supplied, I would use double-quotes around each, and a single space between arguments. Using the long string syntax [[...]]
has the advantage that backslash isn't a special character, so you don't need extra leaning toothpicks making it harder to read the string literal.
Using double quotes around each argument should work on both Windows and *nix, although it is harder to find commands that are the same on both platforms, of course.
One other detail to be aware of is that \Programs Files
might not be on C:
. There might not even be a disk named C:
. (My work PC boots from E:
and I find more buggy programs that way.) The easiest way to learn the correct pathname is to just use the environment variable ProgramFiles
. There are many other ways.