2016-12-16 46 views
1

我開始在安裝了cygwin(Python 2.7)的Windows 7 x64機器上開發一個小型PDF格式爲jpg腳本。以下作品完美:在Python子進程中使用Windows路徑(指向可執行文件)[beginner]

import subprocess 
filename = "test" 
subprocess.check_output('gs -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT) 

我的Windows 10的x64非Cygwin的機器(Python 2.7版)上拉這個項目後,該代碼的錯誤,因爲它不再承認「GS」作爲內置短路代表ghostscript。所以我嘗試以下:

import subprocess 
filename = "test" 
subprocess.check_output('C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT) 

WindowsError: [Error 2] The system cannot find the file specified 

好的,我是一個初學者,在這裏犯了一些明顯的錯誤。 .20\bin\之間"\b"字符突出顯示...指示我我不知道這是一個單一的路徑。

事情我自己也嘗試(獨立運行):

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT') 

subprocess.check_output("C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT/s", shell=True) 

我已閱讀: * How to use the dir/s command in Python? * How to execute a command prompt command from python * wrapping cmd.exe with subprocess * Python subprocess does not take correct arguments * wrapping cmd.exe with subprocess無濟於事:(

總是給出的錯誤是:

Traceback (most recent call last): 
File "C:/Python/Project/projectx/manual_conversion/manual_conversion.py", line 16, in <module> 
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT) 
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 567, in check_output 
process = Popen(stdout=PIPE, *popenargs, **kwargs) 
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 711, in __init__ 
errread, errwrite) 
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 959, in _execute_child 
startupinfo) 
WindowsError: [Error 2] The system cannot find the file specified 

我完全理解這是菜鳥的東西+切換到UNIX會讓我的生活更輕鬆 - 但在我的公司環境中,我並沒有完全擁有這個選項。任何幫助,將不勝感激!

第一個問題,如果需要的話指出我的任何改變!

編輯1:我也嘗試:

subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf') 

因爲我想,也許我的變量級聯被打破了命令......但是這又產生了同樣的錯誤。

編輯2:傳遞充分論證的列表

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf']) 

產生同樣的錯誤。但這:

subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe']) 

能在Windows啓動ghostscript的。謝謝@lit讓我嘗試的東西,現在新的問題是如何將選項傳遞給subprocess'check_output executable :)。

仍然困擾我的是多麼容易的工作在Windows系統中,只是因爲它已經安裝了cygwin,不知怎的,修改CMD命名空間承認「GS」 ......

+0

當您使用原始字符串r'...'時,結果如何?請複製並粘貼輸出/錯誤消息。 – lit

+0

@lit我不能編輯註釋...但我將它添加到原始問題,thnx! – MarcelTon

+2

請閱讀'subprocess.check_output()'的文檔。第一個參數應該是一個「列表」。如果你嘗試'subprocess.check_output([r「C:\ Program Files \ gs \ gs9.20 \ bin \ gwin64c.exe」,「-sDEVICE = jpeg」,「-dPDFFitPage」,「-g2800x3620」,「 -o「,」2800-%03d.jpg test.pdf「])' – lit

回答

0

路徑包含這裏Program Files一個空間。通常,空間意味着路徑的末端。 在路徑周圍使用引號"會告訴操作系統不要將空間視爲路徑的結尾,而是將引號之間的所有文本都視爲路徑。 因此,只需加上引號:

subprocess.check_output([r'"C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf']) 
+0

由於rep-limit,我無法對此表示滿意,但我做到了! – MarcelTon

+0

您不能upvote,但你可以[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)一個答案,如果它解決了你的問題。 –

相關問題