简体   繁体   English

在 Python 中使用 cat 命令进行打印

[英]Using cat command in Python for printing

In the Linux kernel, I can send a file to the printer using the following command在 Linux kernel 中,我可以使用以下命令将文件发送到打印机

cat file.txt > /dev/usb/lp0

From what I understand, this redirects the contents in file.txt into the printing location.据我了解,这会将 file.txt 中的内容重定向到打印位置。 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?有一个更好的方法吗?

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 . 如果你只想运行shell命令, bashpython要好得多。 On the other hand, if you want to use Python, there are better ways to copy files than shell redirection. 另一方面,如果要使用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模块是一个很好的帮助。

Remember, in UNIX - everything is a file. 请记住,在UNIX中 - 一切都是文件。 Even devices. 甚至设备。

So, you can just use basic (or anything else, eg 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)

PS Please, don't use system() call (or similar) to solve your issue. PS请不要使用system()调用(或类似)来解决您的问题。

under windows OS there is no cat command you should use type instead of cat under windows在 windows 操作系统下没有cat命令你应该在 windows 下使用type而不是cat

(**if you want to run cat command under windows please look at: https://stackoverflow.com/a/71998867/2723298 ) (**如果你想在 windows 下运行cat命令,请查看: https://stackoverflow.com/a/71998867/2723298

import os
os.system('type a.txt > copy.txt')

..or if your OS is linux and cat command didn't work anyway here are other methods to copy file.. with grep: ..或者如果您的操作系统是 linux 并且 cat 命令无论如何都不起作用这里是复制文件的其他方法.. grep:

   import os
    
   os.system('grep "" a.txt > b.txt')

* ' ' are important! * ' '很重要!

copy file with sed:使用 sed 复制文件:

os.system('sed "" a.txt > sed.txt')

copy file with awk:使用 awk 复制文件:

os.system('awk "{print $0}" a.txt > awk.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM