2016-11-08 12 views
0

運行過程中,我希望通過Python運行下面很長的shell命令:使用subprocess.run在Windows

C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe "E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat" pc\ndf\patchable\gfx\everything.ndfbin TAmmunition 

當我運行這個作爲,從Windows外殼程序,它按預期工作。

但是,當我試圖通過Python的subprocess.run做同樣的事情時,它不喜歡它。這是我輸入:

import subprocess 
comm = [r'C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe "E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat" pc\ndf\patchable\gfx\everything.ndfbin TAmmunition'] 
subprocess.run(comm, shell=True) 

這裏是我的輸出:

The directory name is invalid. 
Out[5]: CompletedProcess(args=['C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe "E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat" pc\\ndf\\patchable\\gfx\\everything.ndfbin TAmmunition'], returncode=1) 

爲什麼會發生這種情況?

回答

1

間距錯了。子進程正在等待一系列參數,它將爲你正確地分配空間。

comm = ['C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe','E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat','pc\ndf\patchable\gfx\everything.ndfbin','TAmmunition'] 

你所得到的錯誤的原因是因爲周圍E中的雙引號的:/蒸汽... 它寫這樣的一個shell:

c:/users/alex/desktop/tableexporter/wgtableexporter.exe \"E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat\" pc\ndf\patchable\gfx\everything.ndfbin TAmmunition 
1

的間距不正確,但是使用os.system()將不會要求您更改間距。

這應該工作:

import os 
os.system("""C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe  "E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat" pc\ndf\patchable\gfx\everything.ndfbin TAmmunition""") 

但是,如果你想使用子(這是更好)

import subprocess 
comm = ['C:/Users/Alex/Desktop/tableexporter/WGTableExporter.exe','E:/Steam (Games Installed Directly to Hard Drive)/steamapps/common/Wargame Red Dragon/Data/WARGAME/PC/430000626/NDF_Win.dat','pc\ndf\patchable\gfx\everything.ndfbin TAmmunition'] 
subprocess.run(comm, shell=True)