189

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?

1

17 Answers 17

222

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, platform
if platform.system() == 'Darwin':       # macOS
    subprocess.call(('open', filepath))
elif platform.system() == 'Windows':    # Windows
    os.startfile(filepath)
else:                                   # linux variants
    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.

7
  • 6
    Using 'start' in subprocess.call() doesn't work on Windows -- start is not really an executable. Oct 18, 2009 at 19:10
  • 4
    nitpick: on all linuxen (and I guess most BSDs) you should use xdg-open - linux.die.net/man/1/xdg-open
    – gnud
    Oct 18, 2009 at 19:57
  • 8
    start on Windows is a shell command, not an executable. You can use subprocess.call(('start', filepath), shell=True), although if you're executing in a shell you might as well use os.system. Feb 3, 2011 at 23:25
  • I ran xdg-open test.py and it opened firefox download dialog for me. What's wrong? I'm on manjaro linux.
    – Jason
    Sep 12, 2018 at 3:28
  • 1
    @Jason Sounds like your xdg-open configuration is confused, but that's not really something we can troubleshoot in a comment. Maybe see unix.stackexchange.com/questions/36380/…
    – tripleee
    May 8, 2019 at 6:20
103

open and start are command-interpreter things for Mac OS/X and Windows respectively, to do this.

To call them from Python, you can either use subprocess module or os.system().

Here are considerations on which package to use:

  1. You can call them via os.system, which works, but...

    Escaping: os.system only works with filenames that don't have any spaces or other shell metacharacters in the pathname (e.g. A:\abc\def\a.txt), or else these need to be escaped. There is shlex.quote for Unix-like systems, but nothing really standard for Windows. Maybe see also python, windows : parsing command lines with shlex

    • MacOS/X: os.system("open " + shlex.quote(filename))
    • Windows: os.system("start " + filename) where properly speaking filename should be escaped, too.
  2. You can also call them via subprocess module, but...

    For Python 2.7 and newer, simply use

    subprocess.check_call(['open', filename])
    

    In Python 3.5+ you can equivalently use the slightly more complex but also somewhat more versatile

    subprocess.run(['open', filename], check=True)
    

    If you need to be compatible all the way back to Python 2.4, you can use subprocess.call() and implement your own error checking:

    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 using subprocess?

    • Security: 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 interpret, 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 little additional protection.
    • Error handling: It doesn't actually give us any more error detection, we're still depending on the retcode in either case; but the behavior to explicitly raise an exception in the case of an error will certainly help you notice if there is a failure (though in some scenarios, a traceback might not at all be more helpful than simply ignoring the error).
    • Spawns a (non-blocking) subprocess: We don't need to wait for the child process, since we're by problem statement starting a separate process.

    To the objection "But subprocess is preferred." However, os.system() is not deprecated, and it's in some sense the simplest tool for this particular job. Conclusion: using os.system() is therefore also a correct answer.

    A marked disadvantage is that the Windows start command requires you to pass in shell=True which negates most of the benefits of using subprocess.

22
  • 3
    Depending where filename comes form, this is a perfect example of why os.system() is insecure and bad. subprocess is better. Mar 28, 2009 at 16:36
  • 7
    Nick's answer looked fine to me. Nothing got in the way. Explaining things using wrong examples isn't easily justifiable. Mar 29, 2009 at 17:10
  • 2
    It's less secure and less flexible than using subprocess. That sounds wrong to me. Mar 29, 2009 at 18:35
  • 8
    Of course it matters. It's the difference between a good answer and a bad answer (or a terrible answer). The docs for os.system() themselves say "Use the subprocess module." What more is needed? That's deprecation enough for me. Mar 30, 2009 at 19:46
  • 20
    I feel a bit reluctant to restart this discussion, but I think the "Later update" section gets it entirely wrong. The problem with os.system() is that it uses the shell (and you are not doing any shell escaping here, so Bad Things will happen for perfectly valid filenames that happen to contain shell meta-characters). The reason why subprocess.call() is preferred is that you have the option to bypass the shell by using subprocess.call(["open", filename]). This works for all valid filenames, and doesn't introduce a shell-injection vulnerability even for untrusted filenames. Apr 23, 2012 at 14:55
62

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
  • 1
    Thanks. I didn't notice the availability, since the docs have it appended to the last paragraph. In most other sections, the availability note occupies its own line. Jan 12, 2009 at 17:14
  • 1
    On Linux for some reason, rather than raising an error, the startfile function doesn't even exist, which means that users will get a confusing error message about a missing function. You might want to check the platform to avoid this.
    – c z
    Feb 14, 2020 at 10:20
  • os.startfile supports pathlib like objects whereas other filename based solutions do not
    – Phillmac
    Oct 23, 2020 at 10:10
43

Just for completeness (it wasn't in the question), xdg-open will do the same on Linux.

2
  • 8
    +1 Usually, responders should not answer questions that were not asked, but in this case I think it is very relevant and helpful for the SO community as a whole.
    – demongolem
    Nov 3, 2012 at 15:09
  • was looking for this
    – nurettin
    Mar 21, 2020 at 9:00
26
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])
2
  • 2
    Huh, I didn't know about startfile. It would be nice if the Mac and Linux versions of Python picked up similar semantics.
    – Nick
    Jan 12, 2009 at 18:27
  • 3
    Relevant python bug: bugs.python.org/issue3177 - provide a nice patch, and it might get accepted =)
    – gnud
    Oct 18, 2009 at 20:01
23

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
  • 3
    As far as I can tell, this is by far the best answer. Seems cross-platform and no need to check which platform is in use or import os, platform.
    – polandeer
    May 8, 2013 at 23:37
  • 2
    @jonathanrocher: I see Mac support in the source code. It uses open location there that should work if you give the path as a valid url.
    – jfs
    Aug 7, 2015 at 19:14
  • 1
    macOS: import webbrowser webbrowser.open("file:///Users/nameGoesHere/Desktop/folder/file.py")
    – user5306470
    Dec 17, 2017 at 22:53
  • 4
    docs.python.org/3/library/webbrowser.html#webbrowser.open "Note that on some platforms, trying to open a filename using [webbrowser.open(url)], may work and start the operating system’s associated program. However, this is neither supported nor portable."
    – nyanpasu64
    Mar 25, 2019 at 22:39
7

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.

5

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.

1
  • 1
    Another workaround is to give it a title in quotes, e.g. start "Title" "C:\long path to\file.avi" May 21, 2014 at 13:45
5

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 correctly and when the i18n exist in the directory, os.system needs to change the unicode to the codec of the console in Windows.

1
  • Works perfectly in Python 3 !
    – luca76
    Aug 11, 2020 at 10:40
5

Here is the answer from Nick, adjusted slightly for WSL:

import os
import sys
import logging
import subprocess

def get_platform():
    if sys.platform == 'linux':
        try:
            proc_version = open('/proc/version').read()
            if 'Microsoft' in proc_version:
                return 'wsl'
        except:
            pass
    return sys.platform

def open_with_default_app(filename):
    platform = get_platform()
    if platform == 'darwin':
        subprocess.call(('open', filename))
    elif platform in ['win64', 'win32']:
        os.startfile(filename.replace('/','\\'))
    elif platform == 'wsl':
        subprocess.call('cmd.exe /C start'.split() + [filename])
    else:                                   # linux variants
        subprocess.call(('xdg-open', filename))
3

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.

1
  • 1
    how it compares to os.startfile(file)?
    – jfs
    Apr 23, 2016 at 11:28
2

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]")

2

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))
1

On mac os you can call open:

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

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

1

I built a small library combining the best answers here for cross-platform support:

$ pip install universal-startfile

then launch a file or URL:

from startfile import startfile

startfile("~/Downloads/example.png")
startfile("http://example.com")
0

I think you might want to open file in editor.

For Windows

subprocess.Popen(["notepad", filename])

For Linux

subprocess.Popen(["text-editor", filename])
0

I was getting an error when calling my open file() function. I was following along with a guide but the guide was written in windows while I'm on Linux. So the os.statrfile method wasn't working for me. I was able to alleviate this problem by doing the following:

Import libraries

import sys, os, subprocess
import tkinter
import tkinter.filedioalog as fd
import tkinter.messagebox as mb

After the lib imports I then called the subprocess method for opening a file in unix based OS which is "xdg-open" and the file that will be opened.

def open_file():
     file = fd.askopenfilename(title='Choose a file of any type', filetypes=[('All files', "*.*")])
     subprocess.call(['xdg-open', file])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.