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?
Asked
Active
Viewed 1.1e+01k times
29

Sayed Sohan
- 1,385
- 16
- 23

Aleksa
- 2,976
- 4
- 30
- 49
4 Answers
32
Try:
import subprocess
subprocess.Popen([file],shell=True)

Gustave Coste
- 677
- 5
- 19

Daniel Wärnå
- 678
- 8
- 17
-
5Since 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
18
import os
os.startfile(filename)

Raj Stha
- 1,043
- 11
- 18
-
11This works only in Windows. (`startfile` is not available in Linux) – loved.by.Jesus Jun 26 '18 at 15:12
-
This didn't work for me on Windows, just kind of crashes the application. However, the solution using subprocess did work. Thanks – jacktrader Aug 10 '22 at 14:00
-6
This is a bit late but nobody mentioned:
open("file_name.pdf")

Astrophe
- 568
- 1
- 7
- 25
-
13The reason that no one mentioned that is because it is not what OP wanted. That will open the file internally in Python to be edited etc. It will not open the file in a program such as Adobe Acrobat, which is what they wanted. – Nat Cecil Jul 09 '19 at 10:42
-
2
-
4
-