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 8.9k times
28
Sayed Sohan
- 1,135
- 13
- 22
Aleksa
- 2,764
- 4
- 27
- 45
4 Answers
28
Try:
import subprocess
subprocess.Popen([file],shell=True)
Gustave Coste
- 622
- 7
- 17
Daniel Wärnå
- 638
- 9
- 17
-
2Since 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
17
import os
os.startfile(filename)
Raj Stha
- 915
- 10
- 17
-
8This works only in Windows. (`startfile` is not available in Linux) – loved.by.Jesus Jun 26 '18 at 15:12
-4
This is a bit late but nobody mentioned:
open("file_name.pdf")
Astrophe
- 511
- 1
- 6
- 21
-
11The 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
-
1
-