[Tutor] Something new in line printing?

Alan Gauld alan.gauld at yahoo.co.uk
Mon Nov 11 19:13:06 EST 2019


On 11/11/2019 22:51, Ken Green wrote:
> I have not use Python printing for quite a while.
> In the meantime, I acquired a new printer, a HP
> OfficeJet 4652. Ran a program to print the output
> of a program and get the following error:

This is as much an OS problem as it is python so it helps
if you start off by telling us which OS you are using.
I assume some kind of Unix derivative but which?

> lpr: Error - No default destination.

This is an OS level error not a Python issue.

> # sample_printing.py
> # 11/11/2019
> 
> import sys, os, time
> 
> print

This implies you are using Python 2.7?
Might be worth considering a move to python 3 before support
for v2 ends next year,.

> datecode = "02042011"
> month = datecode[0:2]
> date?? = datecode[2:4]
> year?? = datecode[4:8]
> datecode = year + month + date

Consider using the datetime module functions for date
processing. But in this example slicing suffices I
guess.

> pr = (os.popen("lpr", "w"))

os.popen is deprecated, the subprocess module is the recommended
tool for this kind of task. It is much more powerful and flexible
although somewhat more complex to use. But the module
documentation gives several examples for most scenarios,
including os.pipe.

> pr.write (datecode)
> pr.close()

What happens here depends on your OS setup...
Does it work outside python if you just cat a string into lpr?

> Did something changed?

Yes, your printer.
Until we know that it is set up to work properly with lpr
all bets are off.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list