Subtime Text 2 - cannot run a Windows command with spaces and quotes

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 does not execute 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 it is executed if you manually copy and paste the command (output by this script) into cmd.exe .

How can I make it work?

+6
source share
5 answers

This is a problem with the space in your file path (this is a problem on Windows). os.system () opens a command shell, and this behavior is inherited from the command shell. If you open the "DOS" window and enter the same thing happens with it, you will get the same results - these are Windows shells that require quotes with built-in spaces. You must use another pair of quotation marks.

+2
source

See this answer , The elevated part is auxiliary, but from the python interpreter you can verify that the os.system will work if you use double quotes throughout the string. However, you do not need quotes if you use Popen (it is smart enough to figure it out).

for instance

 >>> 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, in spite of your comments, do not double quote along the way, order the entire command ""path to exe with spaces" "arg1" "arg2" "arg3"" , and you really don't need all the internal quotes, but they don’t will hurt, which means it should work with ""path to exe with spaces arg1 arg2 arg3""

+1
source

Like others, the subprocess module is probably the best solution.

However, it is worth noting that MS Windows still provides an old 8.3 filename translation for compatibility with older MS-DOS programs. You can find the translations by running dir /X in cmd.exe , which usually shows that C:\Program Files can be called C:\Progra~1 .

In your case, the following ...

 import os path = 'C:\\Progra~1\\SumatraPDF\\SumatraPDF.exe' params = ' -page 5 ' arg = 'D:\\Dropbox\\FinalT~1\\FinalD~1.pdf' cmd = path + params + arg print cmd os.system(cmd) 

... must work.

+1
source

os.system deprecated. The right thing to do is Wyrmwood mentioned by Popen , and separate the arguments:

 import subprocess subprocess.Popen([ "C:\Program Files\SumatraPDF\SumatraPDF.exe", "-page", "5", "D:\Dropbox\Final Term\Final Draft.pdf" ]) 

You can then create your team by specifying a list and .extend with your arguments. You can add ( + ) sys.argv to take from the command line, etc.


There are other useful functions in subprocess that allow you, for example, to get output from a command ( checkoutput ). You probably just want subprocess.call .

If you use plain process = subprocess.Popen(...) , be prepared to call process.wait() to prevent premature termination.

0
source

It seems to work for me. Quote the executable path and then quote the entire line.

 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(quoted(path) + params + arg) print(cmd) os.system(cmd) 
0
source

Source: https://habr.com/ru/post/972882/


All Articles