Python Forum

Full Version: Print the file using the associated application on Linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Based on this post I want to know whether it is possible to send file to default printer using the associated on Linux?
I've tested this exampe on Windows and it works fine! So, I can create any file (.txt, .docx, .xlxs e.t.c.) and python script automatically opens it with associated application and prints it immediately.

#For Windows
import os
os.startfile("C:/Users/TestFile.txt", "print")
I've already tested other code sample on Linux and it works but not as I would like. The issue is that it prints only string but not the file itself. I looked at several other examples and it seems that this function can send for printing the file content only, am I right?

#For Linux
import subprocess
lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write("Some text to print here...")
I want to make it work for Linux as well as it works for Windows. Is it possible? I really don't want to make complex mouse automation just for file printing on Linux. Plz help noob like me Big Grin
man lpr

$ lpr <options> file

So with subprocess is going to be subprocess.call(['lpr', '-one-option', '-second-option', 'file_path/file'])