使用 subprocess.run 在 Windows 上运行进程 [英] Using subprocess.run to run a process on Windows

查看:109
本文介绍了使用 subprocess.run 在 Windows 上运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过 Python 运行以下非常冗长的 shell 命令:

I wish to run the following very lengthy shell command via Python:

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 shell 按原样运行它时,它按预期工作.

When I run this as-is from the Windows shell, it works as expected.

但是,当我尝试通过 Python 的 subprocess.run 执行相同操作时,它不喜欢它.这是我的输入:

However, when I attempt to do the same via Python's subprocess.run, it doesn't like it. Here is my input:

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)

为什么会出现这种情况?

Why does this occur?

推荐答案

间距不对.子进程需要一个参数列表,它将为您正确分配.

The spacing is wrong. subprocess is expecting a list of arguments which it will space out correctly for you.

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:/steam 周围的双引号...它正在向 shell 写入这样的内容:

The reason you are getting the error is because of the doublequotes around e:/steam... It's writing something like this to a 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

这篇关于使用 subprocess.run 在 Windows 上运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆