28

I wanna open pdf file from python console, I can do it with os.system(filename), it will open in adobe reader, but the problem is that os.system also opens a command prompt, is there another way that won't open command prompt?

Sayed Sohan
  • 1,135
  • 13
  • 22
Aleksa
  • 2,764
  • 4
  • 27
  • 45

4 Answers4

28

Try:

import subprocess
subprocess.Popen([file],shell=True)
Gustave Coste
  • 622
  • 7
  • 17
Daniel Wärnå
  • 638
  • 9
  • 17
  • 2
    Since the file is not executable is might not always work ("permission denied"), you might want to use the solution I found [here](https://raspberrypi.stackexchange.com/questions/87597/problem-with-subprocess-popen-permission-denied#comment138914_87597) (`subprocess.call(["xdg-open", file])`) – David Beauchemin Jun 02 '21 at 17:51
  • Thank you David for this pointer to subprocess.call. This was the solution I needed to start up via the file type. – Anthony Petrillo Oct 27 '21 at 16:52
18
import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')
Static Void
  • 498
  • 3
  • 8
17
import os
os.startfile(filename)
Raj Stha
  • 915
  • 10
  • 17
-4

This is a bit late but nobody mentioned:

open("file_name.pdf")
Astrophe
  • 511
  • 1
  • 6
  • 21