Linux 在 Python 中使用 cat 命令进行打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15169133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using cat command in Python for printing
提问by user2125538
In the Linux kernel, I can send a file to the printer using the following command
在 Linux 内核中,我可以使用以下命令将文件发送到打印机
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
据我了解,这会将 file.txt 中的内容重定向到打印位置。我尝试使用以下命令
>>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?
有一个更好的方法吗?
采纳答案by abarnert
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.
虽然您的代码没有理由不工作,但这可能不是您想要的方式。如果你只是想运行 shell 命令,bash
比python
. 另一方面,如果你想使用 Python,有比 shell 重定向更好的复制文件的方法。
The simplest way to copy one file to another is to use shutil
:
将一个文件复制到另一个文件的最简单方法是使用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.
显然,一个真正的程序会更复杂一些——它会用不同的命令做不同的事情,并且可能接受告诉它要打印哪个文件的参数,等等。如果你想走那条路,这个cmd
模块是一个很大的帮助。
回答by Nikolay Baluk
Remember, in UNIX - everything is a file. Even devices.
请记住,在 UNIX 中 - 一切都是文件。甚至设备。
So, you can just use basic (or anything else, e.g. shutil.copyfile) files methods (http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files).
所以,你可以只使用基本的(或其他任何东西,例如shutil.copyfile)文件方法(http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files)。
In your case code may(just a way) be like that:
在您的情况下,代码可能(只是一种方式)是这样的:
# Read file.txt
with open('file.txt', 'r') as content_file:
content = content_file.read()
with open('/dev/usb/lp0', 'w') as target_device:
target_device.write(content)
P. S. Please, don't use system() call (or similar) to solve your issue.
PS 请不要使用 system() 调用(或类似方法)来解决您的问题。