使用subprocess.run在Windows上运行一个进程



我希望通过Python运行以下非常长的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" pcndfpatchablegfxeverything.ndfbin TAmmunition

当我在Windows shell中按原样运行它时,它按预期工作。

然而,当我试图通过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" pcndfpatchablegfxeverything.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)

为什么会出现这种情况?

空格错误。子进程正在等待一个参数列表,它将为你正确地间隔。

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','pcndfpatchablegfxeverything.ndfbin','TAmmunition']

你得到错误的原因是由于双引号周围的e:/steam…它在向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" pcndfpatchablegfxeverything.ndfbin TAmmunition

空格不正确,但是使用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" pcndfpatchablegfxeverything.ndfbin TAmmunition""")

但是,如果您想使用subprocess(这更好)

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','pcndfpatchablegfxeverything.ndfbin TAmmunition']
subprocess.run(comm, shell=True)

相关内容

  • 没有找到相关文章

最新更新