【发布时间】:2016-09-13 15:07:26
【问题描述】:
对于我自己的一个小项目,我正在尝试编写一个程序,在计算机默认打印机上打印出文件的内容。 我知道周围有很多类似的问题,但它们都不适用于我的电脑(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
【问题讨论】:
-
为什么是
StringIO而不是open? -
@Moses Koledoye 您的评论修复了这两个错误 :) 我在写答案的第二部分时意识到。
-
StringIO的东西一开始也让检测重复变得更加困难。 -
我只使用了我从谷歌得到的代码,所以不要问我为什么不打开大声笑
标签: python python-2.7 printing