Using cat command in Python for printing
While there's no reason your code shouldn't work, this probably isn't the way you want to do this. If you just want to run shell commands, bash
is much better than python
. On the other hand, if you want to use Python, there are better ways to copy files than shell redirection.
The simplest way to copy one file to another is to use shutil
:
shutil.copyfile('file.txt', '/dev/usb/lp0')
(Of course if you have permissions problems that prevent redirect from working, you'll have the same permissions problems with copying.)
You want a program that reads input from the keyboard, and when it gets a certain input, it prints a certain file. That's easy:
import shutil
while True:
line = raw_input() # or just input() if you're on Python 3.x
if line == 'certain input':
shutil.copyfile('file.txt', '/dev/usb/lp0')
Obviously a real program will be a bit more complex—it'll do different things with different commands, and maybe take arguments that tell it which file to print, and so on. If you want to go that way, the cmd
module is a great help.
user2125538
Updated on October 03, 2020Comments
-
user2125538 almost 4 years
In the Linux kernel, I can send a file to the printer using the following command
cat file.txt > /dev/usb/lp0
From what I understand, this redirects the contents in file.txt into the printing location. I tried using the following command
>>os.system('cat file.txt > /dev/usb/lp0')
I thought this command would achieve the same thing, but it gave me a "Permission Denied" error. In the command line, I would run the following command prior to concatenating.
sudo chown root:lpadmin /dev/usb/lp0
Is there a better way to do this?
-
user2125538 over 11 yearsI would like the program to run shell commands once it responds to certain inputs. Would bash still be better than python for this?
-
abarnert over 11 years@user2125538: If you explicitly want to "run shell commands" rather than "do some stuff", then it may well be. But without knowing more about what you're actually trying to build, it's very hard to say.
-
user2125538 over 11 yearsTo explain it briefly, what I would like is a program that once it reads a certain input from a keyboard, it sends a file at a certain location to the printer.
-
abarnert over 11 yearsSo you don't need to run shell commands, you need to print files. Let me edit my answer to show how you would write such a program.
-
tshepang over 11 yearsbut the second one is not even recommended; first way is always preferred
-
Nikolay Baluk over 11 yearsWhat you mean no recommended? Can you provide a proof? It's a basic I/O operation!
-
user2125538 over 11 years@NikolayBaluk Just out of curiousity, can you explain to me the difference between your solution and the command using the 'shutil' command?
-
Nikolay Baluk over 11 yearsIt works, yes. Now make sure, that you are not really import whole module for one operation.
-
tshepang over 11 yearsOther than it being uglier, if your
write
method fails, there won't be any closing of the file. It's a bit of over-simplifying since the OS may take care of it of course. -
Nikolay Baluk over 11 years@user2125538 sure, look. In my, you don't import anything (works faster and is native). With shutil, you need to import something and you can deal only with files (you can not write simple string to device), BUT it have much more convenient code.
-
abarnert over 11 years@NikolayBaluk: What do you mean by that? Importing standard library modules is a perfectly reasonable thing to do. If you're twisting yourself into knots to avoid it, you're not writing Python.
-
user2125538 over 11 years@abarnert I asked a similar question to Nikolay, but can you explain to me the difference between your solution and the command using the "opening and closing" commands Nikolay provided?
-
abarnert over 11 years@NikolayBaluk: I'm willing to bet that
shutil.copyfile
will be a whole lot faster than reading the whole file into memory so you can write it out. (And it will also take a lot less memory, obviously.) And the cost of importing a module is so tiny compared to the cost of copying a file (and then waiting for a printer to print it!) that it's not even going to be measurable. -
Nikolay Baluk over 11 yearsI mean "from shutil import copyfile". Even if you're "writing Python" be honestly remember about performance.
-
abarnert over 11 years@user2125538: Using
shutil
is faster, it uses less memory, it's easier to read, it's harder to get wrong, and it's generally considered more pythonic to use the "batteries included" than to reinvent the wheel. -
Nikolay Baluk over 11 years@abarnert correct idea. To avoid reading whole file you may just read-write by line, but in this case, is definitely better to use shutil.copyfile.
-
abarnert over 11 years@NikolayBaluk: I have a feeling you've never actually timed, profiled, or optimized any real-life code, if you really think the cost of
import shutil
is going to be anywhere near the cost of reading an entire file into memory unnecessarily. -
Nikolay Baluk over 11 years@abarnert agree with you. Just don't import whole module.
-
Nikolay Baluk over 11 years@abarnert oh man... yes! it's better than a whole file in memory, but worse than 'from shutil import copyfile'. And stop talking about me, please.
-
abarnert over 11 yearsWhat do you mean "don't import whole module"? Doing
from shutil import copyfile
is not any faster thanimport shutil
(in fact, it's actually a tiny bit slower, but not enough that you'd ever notice). And it's generally not considered good style in real programs (although in a tiny script like this, it might be reasonable). Let me guess: do you not understand the difference betweenimport shutil
andfrom shutil import *
? -
abarnert over 11 years@NikolayBaluk: OK, I'm glad you've read that. Now do you understand why you should not recommend
from shutil import copyfile
overimport shutil
?