Python打印到/dev/usb/lp0

3

我希望从我的Python代码中打印到/dev/usb/lp0。在中,它非常简单:echo 'apple' >/dev/usb/lp0

我找到了linemode,但它无法使用。当我试图安装它时,我会得到以下错误。

byte-compiling build/bdist.linux-x86_64/egg/linemode/renderers/xml.py to xml.pyc
File "build/bdist.linux-x86_64/egg/linemode/renderers/xml.py", line 67
def __init__(self, source, *, max_width=None, prelude=True):
                            ^
SyntaxError: invalid syntax

我如何从Python代码中打印到/dev/usb/lp0

如果你想将某些东西发送到打印机,这里有一个解决方案: https://dev59.com/YWrWa4cB1Zd3GeqP6Qjo - Striver
Striver:如果我想写入文件,这没问题。但是我想使用 /dev/usb/lp0,然后我会得到错误:Permission denied: '/dev/usb/lp0'。 - user3740961
您可以创建一个普通的文本文件,然后使用lpr命令行工具打印此文件。 - Alexander Baltasar
Alexander Baltasar:这不是一个优美的解决方案。 - user3740961
我创建了一个普通的文本文件,并编写了以下Python代码:subprocess.call("cat file > /dev/usb/lp0", shell=True) 但是它没有起作用(没有错误)。但是这个Bash命令可以正常工作:cat file > /dev/usb/lp0。为什么? - user3740961
1个回答

5

这是有效的:

 with open('/dev/usb/lp0', 'w') as printer:
            printer.write("Line 1\n")
            printer.write("Line 2\n")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接