2

I currently have the following code:

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

The program png2pos is being accessed because it's giving me the message:

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

This is the same error I get if I forget to type in > /dev/usb/lp0, so I'm fairly certain it has something to do with the '>' character. How would one redirect this output to /dev/usb/lp0 with subprocess?

theovenbird
  • 324
  • 1
  • 4
  • 10

2 Answers2

3

To make sure the output is redirected properly, you need to set shell to True and pass a single string:

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

Otherwise ">" is treated as a program argument.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • there is no need to use `shell=True` here. See answers in the linked duplicate. – jfs Nov 03 '15 at 15:32
2

I do not have your tool installed, so I cannot really test here. But had an issue with redirecting output from a console application using python before. I had to redirect it using the command itself, not via the shell (as you are trying)

with open("/dev/usb/lp0", 'wb') as output_str:
    subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)
Ward
  • 2,802
  • 1
  • 23
  • 38