Open a text file using notepad as a help file in python?

Solution 1:

import webbrowser
webbrowser.open("file.txt")

Despite it's name it will open in Notepad, gedit and so on. Never tried it but it's said it works.

An alternative is to use

osCommandString = "notepad.exe file.txt"
os.system(osCommandString)

or as subprocess:

import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])

but both these latter cases you will need to find the native text editor for the given operating system first.

Solution 2:

os.startfile('file.txt')

From the python docs:

this acts like double clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

This way if your user changed their default text editor to, for example, notepad++ it would use their preference instead of notepad.

Solution 3:

If you'd like to open the help file with the application currently associated with text files, which might not be notepad.exe, you can do it this way on Windows:

import subprocess
subprocess.call(['cmd.exe', '/c', 'file.txt'])