2016-11-08 14 views
0

上でプロセスを実行するためにsubprocess.runを使用した:私は、Pythonを経由して、以下の非常に長いシェルコマンドを実行したいの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'] 

あなたがエラーを取得している理由は、電子の周りに二重引用符である:/スチーム... それはシェルにこのような何かを書いています:

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) 
関連する問題