运行 linux 命令在 python 3

Run linux command in python 3

运行 linux bash 中的以下命令成功:

gimp --no-interface -b '(python-fu-scale RUN-NONINTERACTIVE 0 0 "img.jpg")' -b '(gimp-quit 0)'

输出:

batch command executed successfully

已打印且子进程退出

运行 它与 python3.5 失败:

import subprocess
subprocess.run("gimp --no-interface -b '(python-fu-scale RUN-NONINTERACTIVE 0 0 \"img.jpg\")' -b '(gimp-quit 0)'".split())

输出结果:

batch command executed successfully
batch command executed successfully

子进程卡住了。

我不确定有什么区别以及如何在 python 中实现等效行为。 有没有办法将 运行 bash 命令作为字符串?

使用以下来源 - Calling an external command in Python 帮助找到答案

import subprocess
import shlex
subprocess.run(shlex.split("gimp --no-interface -b '(python-fu-scale RUN-NONINTERACTIVE 0 0 \"img.jpg\")' -b '(gimp-quit 0)'"))