Skip to main content
 首页 » 编程设计

python之使用默认打印机打印文本文件

2024年10月17日8java哥

<分区>

对于我自己的一个小项目,我正在尝试编写一个程序来在计算机默认打印机上打印出文件的内容。 我知道周围有很多类似的问题,但没有一个适用于我的电脑 (Linux mint 17.3)

这是我试过的,它最接近我需要的:

from subprocess import Popen 
from cStringIO import StringIO 
 
# place the output in a file like object 
sio = StringIO("test.txt") 
 
# call the system's lpr command 
p = Popen(["lpr"], stdin=sio, shell=True) 
output = p.communicate()[0] 

这给了我以下错误:

Traceback (most recent call last): 
  File "/home/vandeventer/x.py", line 8, in <module> 
    p = Popen(["lpr"], stdin=sio, shell=True) 
  File "/usr/lib/python2.7/subprocess.py", line 702, in __init__ 
    errread, errwrite), to_close = self._get_handles(stdin, stdout, stderr) 
  File "/usr/lib/python2.7/subprocess.py", line 1117, in _get_handles 
    p2cread = stdin.fileno() 
AttributeError: 'cStringIO.StringI' object has no attribute 'fileno' 

有没有人知道可以在 python 中实现这个?它真的不必在 Windows 上工作

问候

Cid-El