I have a small Python script generating a Windows command:
def quoted(s):
return '"' + s + '"'
import os
path = 'C:\\Program Files\\SumatraPDF\\SumatraPDF.exe'
params = ' -page 5 '
arg = 'D:\\Dropbox\\Final Term\\Final Draft.pdf'
cmd = quoted(path) + params + quoted(arg)
print cmd
os.system(cmd)
This doesn't run inside Sublime Text 2 (pressing Ctrl+B):
"C:\Program Files\SumatraPDF\SumatraPDF.exe" -page 5 "D:\Dropbox\Final Term\Final Draft.pdf"
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
[Finished in 0.1s]
but runs if I manually copy and paste the command (outputted by this script) into cmd.exe
.
How do I make it work?
That's an issue with space in your filepath (it's a problem on Windows). os.system() opens a command shell, and this behavior is inherited from your command shell. If you open a "DOS box" and type the same things at it, you'll get the same results - it's the Windows command shells that require quoting paths with embedded spaces. You should use another pair of quotation marks.
See this answer, The sublime part is ancillary, but from a python interpreter, you can test that os.system will work if you use double quotes around the whole string. However, you don't need any quotes if you use Popen (it is smart enough to figure that out).
For example
>>> cmd = '""C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe""'
>>> os.system(cmd)
or
>>> cmd = 'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
>>> Popen(cmd)
By the way, from looking at your comments, don't double quote the path, double quote the entire command ""path to exe with spaces" "arg1" "arg2" "arg3""
, and you really don't need all the inner quotes, but they won't hurt, meaning it should work with ""path to exe with spaces arg1 arg2 arg3""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With