【发布时间】:2021-08-19 15:27:24
【问题描述】:
我在从 python 脚本运行一些 cmd 命令时遇到问题。找到很多解释 subprocess.run 的示例和内容,但无法让我的脚本运行...
我有带有类似命令的批处理脚本:
set PATH=%PATH%;"C:\d\program\program_4\bin"
set PROGRAM_WAS_RUNNING=0
echo Starting PROGRAM (headless)
programd --file "C:\d\projects\project1\tool\program.exe" --dont-breakaway-from-job
if %ERRORLEVEL%==0 goto next0
echo -- PROGRAM with GUI is running
set PROGRAM_WAS_RUNNING=1
:next0
..等等
尝试使用此代码但无法正常工作:
command = subprocess.run(["set", "PATH=%PATH%;C:\d\program\program_4\bin"])
command = subprocess.run(["set", "PROGRAM_WAS_RUNNING=0"])
有人可以举个例子并简要说明将所有这些和其他类似批处理命令转换为 Python 的最佳方法吗? 谢谢,
【问题讨论】:
-
set不是程序,而是command specific to thecmdshell。在 Python requires a completely different method 中设置环境变量。您不清楚您到底要做什么/为什么要尝试将此批处理文件移植到 Python,还是要直接通过 Python 运行它? -
我正在尝试直接从 python 脚本运行所有批处理脚本命令
标签: python batch-file command-line subprocess