1

Is there a newer way to open a PDF using an external viewer from Python 3 in Linux other than subprocess?

This sounds like a noobish and duplicate question, but I looked at this question and this question, and all of the answers are over 7 years old and recommended discouraged methods like os.system, old methods like manually creating a subprocess.Popen or Windows-only methods like os.startfile.

So in the time since these questions were answered, have preferred methods of launching a PDF reader from within Python emerged, or are these still the best answers?

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • I will suggest using this guide that I followed a while back https://code.tutsplus.com/tutorials/how-to-work-with-pdf-documents-using-python--cms-25726 – theBrainyGeek Nov 03 '17 at 21:56
  • @theBrainyGeek thanks, clarified my question. I'm looking for something that will open an external viewer, like Evince – jpyams Nov 03 '17 at 21:59
  • See this answer: https://stackoverflow.com/a/435669/399317 – Kos Nov 03 '17 at 22:04
  • Possible duplicate of [Open document with default application in Python](https://stackoverflow.com/questions/434597/open-document-with-default-application-in-python) – Kos Nov 03 '17 at 22:08
  • 1
    So: nope, these are still the best answers, windows still provides a system call accessible as `os.startfile`, linux still provides a command line tool `xdg-open` and mac still provides `open`, you still need to use subprocess with these, and [there's still no standard Python utility](https://bugs.python.org/issue3177) to choose the correct tool for your particular OS. – Kos Nov 03 '17 at 22:15
  • @Kos thanks, your comment answers my question. If you make it an answer I will accept it. – jpyams Nov 03 '17 at 22:18

1 Answers1

3

Python as of 3.6 still doesn't have a cross-platform way to open files using default programs.
Issue 3177 suggested to add one, but it didn't happen yet.

So:

  • On Windows, there's a system call for this, you can reach it from Python via os.startfile,
  • On Linux, there's a command-line tool called xdg-open that does this,
  • On Mac OS, there's a command-line tool simply called open.

This means that unfortunately you still need to check the operating system and pick the right approach. The correct way to call the command-line tools is using the subprocess module.

This answer provides a code snippet:

Open document with default application in Python

Kos
  • 70,399
  • 25
  • 169
  • 233