How can I open files in external programs in Python?

python

Solution

Use this to open any file with the default program:

import os
def openFile():
    fileName = listbox_1.get(ACTIVE)
    os.system("start " + fileName)

If you really want to use a certain program, such as notepad, you can do it like this:

import os
def openFile():
    fileName = listbox_1.get(ACTIVE)
    os.system("notepad.exe " + fileName)

Also if you need some if checks before opening the file, feel free to add them. This only shows you how to open the file.

Problem

I'm wondering how to open files in programs such as Notepad and Picture Viewer depending on the extension the file has. I'm using Python 3.3 on Windows. I've done some research and people have mentioned a module named `Image`, but when I try and import this module I get an ImportError. Here's what I have so far: ``` def openFile(): fileName = listbox_1.get(ACTIVE) if fileName.endswith(".jpg"): fileName.open() ``` I will also have HTML and JSON files that I will need to open in Notepad.

Original source

Related problems