29

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,385
  • 16
  • 23
Aleksa
  • 2,976
  • 4
  • 30
  • 49

4 Answers4

32

Try:

import subprocess
subprocess.Popen([file],shell=True)
Gustave Coste
  • 677
  • 5
  • 19
Daniel Wärnå
  • 678
  • 8
  • 17
  • 5
    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
  • Thanks, it helped me. Just one doubt why do we need shell argument as True here? – S. B. Dec 01 '22 at 06:16
24
import webbrowser
webbrowser.open_new(r'file://C:\path\to\file.pdf')
Static Void
  • 568
  • 3
  • 8
18
import os
os.startfile(filename)
Raj Stha
  • 1,043
  • 11
  • 18
-6

This is a bit late but nobody mentioned:

open("file_name.pdf")
Astrophe
  • 568
  • 1
  • 7
  • 25