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

2024-10-03 11:17:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有以下代码:

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?在


Tags: 代码devgt程序信息pngexamplecall
2条回答

要确保正确重定向输出,需要将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)

相关问题 更多 >