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:18

os.startfile(path, 'open') under windows is good because when spaces exist in the directory, os.system('start', path_name) can't open the app correct and when the i18n exist in the directory, os.system needs to change the unicode to the codec of the console in Windows.

查看更多
一个人的天荒地老
3楼-- · 2018-12-31 16:20

If you have to use an heuristic method, you may consider webbrowser.
It's standard library and despite of its name it would also try to open files:

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable. (Reference)

I tried this code and it worked fine in Windows 7 and Ubuntu Natty:

import webbrowser
webbrowser.open("path_to_file")

This code also works fine in Windows XP Professional, using Internet Explorer 8.

查看更多
不流泪的眼
4楼-- · 2018-12-31 16:21

on mac os you can call 'open'

import os
os.popen("open myfile.txt")

this would open the file with TextEdit, or whatever app is set as default for this filetype

查看更多
柔情千种
5楼-- · 2018-12-31 16:25

Use the subprocess module available on Python 2.4+, not os.system(), so you don't have to deal with shell escaping.

import subprocess, os
if sys.platform.startswith('darwin'):
    subprocess.call(('open', filepath))
elif os.name == 'nt': # For Windows
    os.startfile(filepath)
elif os.name == 'posix': # For Linux, Mac, etc.
    subprocess.call(('xdg-open', filepath))

The double parentheses are because subprocess.call() wants a sequence as its first argument, so we're using a tuple here. On Linux systems with Gnome there is also a gnome-open command that does the same thing, but xdg-open is the Free Desktop Foundation standard and works across Linux desktop environments.

查看更多
旧时光的记忆
6楼-- · 2018-12-31 16:25
import os
import subprocess

def click_on_file(filename):
    '''Open document with default application in Python.'''
    try:
        os.startfile(filename)
    except AttributeError:
        subprocess.call(['open', filename])
查看更多
墨雨无痕
7楼-- · 2018-12-31 16:26

In Mac OS, you can use the "open" command. There is a Windows API call that does something similar, but I don't remember it offhand.

Update

Okay, the "start" command will do it, so this should work.

Mac OS/X:

os.system("open "+filename)

Windows:

os.system("start "+filename)

Much later update by Edward: os.system works, but it only works with filenames that don't have any spaces in folders and files in the filename (e.g. A:\abc\def\a.txt).

Later Update

Okay, clearly this silly-ass controversy continues, so let's just look at doing this with subprocess.

open and start are command interpreter things for Mac OS/X and Windows respectively. Now, let's say we use subprocess. Canonically, you'd use:

try:
    retcode = subprocess.call("open " + filename, shell=True)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e

Now, what are the advantages of this? In theory, this is more secure -- but in fact we're needing to execute a command line one way or the other; in either environment, we need the environment and services to interpet, get paths, and so forth. In neither case are we executing arbitrary text, so it doesn't have an inherent "but you can type 'filename ; rm -rf /'" problem, and IF the file name can be corrupted, using subprocess.call gives us no protection.

It doesn't actually give us any more error detection, we're still depending on the retcode in either case. We don't need to wait for the child process, since we're by problem statement starting a separate process.

"But subprocess is preferred." However, os.system() is not deprecated, and it's the simplest tool for this particular job.

Conclusion: using os.system() is the simplest, most straightforward way to do this, and is therefore a correct answer.

查看更多
登录 后发表回答