2

I'm trying to use python with pycups to print a file.

   import cups
conn = cups.Connection()
printers = conn.getPrinters ()
for printer in printers:
    print printer, printers[printer]["device-uri"]
with open('m.txt', 'w')as output:
    output.write('some text')
    print "done" #debugging
    prin = conn.getDefault()
    conn.printFile(prin, 'm.txt', 'm.txt',{})
    print "done 2" # debugging
    output.close()

It all works up until

conn.printFile(prin, 'm.txt', 'm.txt',{})

where i get an error of

    Traceback (most recent call last):
  File "print.py", line 10, in <module>
    conn.printFile(prin, 'm.txt', 'm.txt',{})
cups.IPPError: (1024, 'No file in print request.')

but when i look m.txt is in my home folder.

im using python 2.7 and xbuntu and have more than one printer and default is set to cups-pdf.

i cant find much info in the docs

shaggs
  • 600
  • 7
  • 27

3 Answers3

0

Try using an absolute path to the file you want to print, ie:

os.path.abspath("m.txt")
totaam
  • 1,306
  • 14
  • 25
0

try to print after you close the file that you write

import os
import cups
conn = cups.Connection()
printers = conn.getPrinters ()
with open('m.txt', 'w')as output:
    output.write('some text')
    print "done" #debugging
    prin = conn.getDefault()
    output.close()
#add script print after close file
f = os.path.abspath("m.txt")
conn.printFile(prin, f, 'm.txt',{})
print "done 2" # debugging

it's work on mine

  • The explicit `output.close()` shouldn't be necessary at all. The whole point with a `with` statement is that the output is closed when the `with` block goes out of scope. – Colin 't Hart Oct 18 '22 at 10:31
0

I think the reason for this error is, you are closing the after printing it, so you need to close the file before printing it.

import cups
conn = cups.Connection()
printers = conn.getPrinters ()
for printer in printers:
    print printer, printers[printer]["device-uri"]
    with open('m.txt', 'w')as output:
        output.write('some text')
        output.close()
        print "done" #debugging
        conn.printFile(printer, 'm.txt', " ", {})
        print "done 2" # debugging
Sunil Jose
  • 315
  • 2
  • 16