Reputation: 1418
I'm trying to use os.system
to invoke a program in C:/Program Files
.
The space in that directory name is messing up every attempt. Here's my code:
cmd = 'C:Program Files\OpenSCAD\openscad.exe -o block0.stl block0.scad'
print cmd
os.system(cmd)
Space ruins things. I've tried about five versions of punctuation (including some recommended in other posts) listed here with the source punctuation, the resulting string as Python sees it, and the results from os.system
.
How do you punctuate this to get it right?
cmd = 'C:Program Files\OpenSCAD\openscad.exe -o block0.stl block0.scad'
C:Program Files\OpenSCAD\openscad.exe -o block0.stl block0.scad
'C:Program' is not recognized as an internal or external command,
operable program or batch file.
cmd = 'C:"Program Files"\OpenSCAD\openscad.exe -o block0.stl block0.scad'
C:"Program Files"\OpenSCAD\openscad.exe -o block0.stl block0.scad
The system cannot find the path specified.
cmd = 'C:"Program Files\OpenSCAD\openscad.exe" -o block0.stl block0.scad'
C:"Program Files\OpenSCAD\openscad.exe" -o block0.stl block0.scad
The system cannot find the path specified.
cmd = 'C:\"Program Files\OpenSCAD\openscad.exe\" -o block0.stl block0.scad'
same thing
cmd = 'C:""Program Files\OpenSCAD\openscad.exe"" -o block0.stl block0.scad'
C:""Program Files\OpenSCAD\openscad.exe"" -o block0.stl block0.scad
'C:""Program' is not recognized as an internal or external command,
operable program or batch file.
cmd = r'C:Program Files\OpenSCAD\openscad.exe -o block0.stl block0.scad'
(recommended here)
C:Program Files\OpenSCAD\openscad.exe -o block0.stl block0.scad
'C:Program' is not recognized as an internal or external command,
operable program or batch file.
Upvotes: 1
Views: 4385
Reputation: 114320
You almost had it a couple of times. The problem is that you either need to put double backslashes, since backslash is the escape character in Python strings, or use raw strings with the r
prefix. In either case, though, you must have a backslash after C:
, and quotes around the portion of the name containing spaces. Any of the following examples should work:
cmd = 'C:\\"Program Files\\OpenSCAD\\openscad.exe" -o block0.stl block0.scad'
cmd = r'C:\"Program Files\OpenSCAD\openscad.exe" -o block0.stl block0.scad'
cmd = "\"C:\\Program Files\\OpenSCAD\\openscad.exe\" -o block0.stl block0.scad"
cmd = r'"C:\Program Files\OpenSCAD\openscad.exe" -o block0.stl block0.scad'
Notice that you won't be able to use double quotes and a raw Python string because you won't be able to escape the double quotes and the path in the string.
Upvotes: 4