3

I would like print to /dev/usb/lp0 from my python code. In bash it is very simple: echo 'apple' >/dev/usb/lp0

I found linemode, but this is not working. When I try to install it I get these errors.

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

How I print to /dev/usb/lp0 from python code?

miken32
  • 42,008
  • 16
  • 111
  • 154
user3740961
  • 329
  • 1
  • 5
  • 17
  • If you want to send something to printer, here is solution: http://stackoverflow.com/questions/13059930/can-you-print-a-file-from-python – Striver Feb 22 '16 at 10:14
  • Striver: If I would like write to file: This ok. But I would like using /dev/usb/lp0 then I get error: Permission denied: '/dev/usb/lp0' – user3740961 Feb 22 '16 at 10:38
  • You can just create a regular textfile and use the lpr commandline tool to print this file. – Alexander Baltasar Feb 22 '16 at 11:09
  • Alexander Baltasar: It's not a pretty solution. – user3740961 Feb 22 '16 at 11:34
  • I created regular textfile and I wrote this python code: subprocess.call("cat file > /dev/usb/lp0", shell=True) But this is not working. (no error) But this bash is working: cat file > /dev/usb/lp0 Why? – user3740961 Feb 22 '16 at 13:15

1 Answers1

5

This is working:

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