Open document with default application in Python

2018-12-31 16:04发布

I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double click on the document icon in Explorer or Finder. What is the best way to do this in Python?

13条回答
刘海飞了
2楼-- · 2018-12-31 16:10

I prefer:

os.startfile(path, 'open')

Note that this module supports filenames that have spaces in their folders and files e.g.

A:\abc\folder with spaces\file with-spaces.txt

(python docs) 'open' does not have to be added (it is the default). The docs specifically mention that this is like double-clicking on a file's icon in Windows Explorer.

This solution is windows only.

查看更多
梦寄多情
3楼-- · 2018-12-31 16:12

Start does not support long path names and white spaces. You have to convert it to 8.3 compatible paths.

import subprocess
import win32api

filename = "C:\\Documents and Settings\\user\\Desktop\file.avi"
filename_short = win32api.GetShortPathName(filename)

subprocess.Popen('start ' + filename_short, shell=True )

The file has to exist in order to work with the API call.

查看更多
皆成旧梦
4楼-- · 2018-12-31 16:14

I am pretty late to the lot, but here is a solution using the windows api. This always opens the associated application.

import ctypes

shell32 = ctypes.windll.shell32
file = 'somedocument.doc'

shell32.ShellExecuteA(0,"open",file,0,0,5)

A lot of magic constants. The first zero is the hwnd of the current program. Can be zero. The other two zeros are optional parameters (parameters and directory). 5 == SW_SHOW, it specifies how to execute the app. Read the ShellExecute API docs for more info.

查看更多
与风俱净
5楼-- · 2018-12-31 16:15

If you want to specify the app to open the file with on Mac OS X, use this: os.system("open -a [app name] [file name]")

查看更多
低头抚发
6楼-- · 2018-12-31 16:15

On windows 8.1, below have worked while other given ways with subprocess.call fails with path has spaces in it.

subprocess.call('cmd /c start "" "any file path with spaces"')

By utilizing this and other's answers before, here's an inline code which works on multiple platforms.

import sys, os, subprocess
subprocess.call(('cmd /c start "" "'+ filepath +'"') if os.name is 'nt' else ('open' if sys.platform.startswith('darwin') else 'xdg-open', filepath))
查看更多
人气声优
7楼-- · 2018-12-31 16:16

If you want to go the subprocess.call() way, it should look like this on Windows:

import subprocess
subprocess.call(('cmd', '/C', 'start', '', FILE_NAME))

You can't just use:

subprocess.call(('start', FILE_NAME))

because start is not an executable but a command of the cmd.exe program. This works:

subprocess.call(('cmd', '/C', 'start', FILE_NAME))

but only if there are no spaces in the FILE_NAME.

While subprocess.call method enquotes the parameters properly, the start command has a rather strange syntax, where:

start notes.txt

does something else than:

start "notes.txt"

The first quoted string should set the title of the window. To make it work with spaces, we have to do:

start "" "my notes.txt"

which is what the code on top does.

查看更多
登录 后发表回答