如何在 python 中子处理此调用:png2pos args > /dev/usb/lp0

How to subprocess this call in python: png2pos args > /dev/usb/lp0

我目前有以下代码:

subprocess.call(["png2pos", "-c", "example_2.png", ">", "/dev/usb/lp0"])

正在访问程序 png2pos,因为它给我消息:

This utility produces binary sequence printer commands. Output have to be redirected

如果我忘记输入 > /dev/usb/lp0,这与我遇到的错误相同,所以我相当确定它与“>”字符有关。如何使用子进程将此输出重定向到 /dev/usb/lp0?

为了确保输出正确重定向,您需要将 shell 设置为 True 并传递一个字符串:

subprocess.call("png2pos -c example_2.png > /dev/usb/lp0", shell=True)

否则">"被视为程序参数。

我没有安装你的工具,所以我不能在这里测试。但是之前使用 python 从控制台应用程序重定向输出时遇到问题。我不得不使用命令本身重定向它,而不是通过 shell (正如你正在尝试的那样)

with open("/dev/usb/lp0", 'wb') as output_str:
    subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)