1

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.
1
  • 1
    Have you tried : `'"C:Program Files\OpenSCAD\openscad.exe" -o block0.stl block0.scad' Commented Jun 17, 2019 at 4:56

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

It worked! I like your second and fourth answers, they make a lot of sense.
Oops! But look what open scad said: Failed to open file C:/Users/Ken/3D/Tetris3D/Files/OpenSCAD/openscad.exe -o block4.stl block4.scad: The system cannot find the path specified.
@Joymaker. Did you put the whole thing in quotes by accident?
This doesn't work for me. My command is: zip_command = r'"C:\Program Files\7-Zip\7z.exe"' I get: 'C:\Program ' is not recognized as an internal or external command, operable program or batch file.
@livefree75. Is that double and single quotes? How do you run the command?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.