24

I would like to give users of my simple program the opportunity to open a help file to instruct them on how to fully utilize my program. Ideally i would like to have a little blue help link on my GUI that could be clicked at any time resulting in a .txt file being opened in a native text editor, notepad for example.

Is there a simple way of doing this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ben
  • 5,525
  • 8
  • 42
  • 66

7 Answers7

63
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.

Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48
  • 3
    +1 for the creativity of thinking of `browser`. I would also suggest looking at [this question on SO](http://stackoverflow.com/questions/434597/open-document-with-default-application-in-python) – inspectorG4dget May 30 '11 at 16:07
  • 3
    Note that using `os.system()` is discouraged now for security reasons. – Philip Jan 25 '14 at 05:45
  • 3
    According to the `webbrowser` documentation: "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." So this probably works on Unix, but may not work on Windows. – Adam Stewart Jun 02 '17 at 20:55
  • 2
    @AdamStewart I am on Mac and it silently does nothing for me. – demongolem May 22 '20 at 12:14
  • 1
    **Webbrowser** does not work on Windows 10 with VSCode. – Timo Dec 29 '20 at 18:29
27
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.

Philip
  • 4,128
  • 5
  • 31
  • 49
  • 3
    This is a good approach, but it is only available on Windows. In a unix environment, you are probably best off using os.system('$EDITOR ') – dusktreader Dec 28 '16 at 21:05
5

You can do this in one line:

import subprocess
subprocess.call(['notepad.exe', 'file.txt'])

You can rename notepad.exe to the editor of your choice.

Abhishek dot py
  • 909
  • 3
  • 15
  • 31
4

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'])
martineau
  • 119,623
  • 25
  • 170
  • 301
  • so, do you mean that, if the system is using notepad++ as default text editor, then my file will be opened using notepad++. am I right? – Thameem Oct 04 '22 at 06:24
3

Here's somewhat of a cross-platform one (edit if you have any other methods):

import shutil, subprocess, os


file_name = "whatever.txt"

if hasattr(os, "startfile"):
  os.startfile(file_name)
elif shutil.which("xdg-open"):
  subprocess.call(["xdg-open", file_name])
elif "EDITOR" in os.environ:
  subprocess.call([os.environ["EDITOR"], file_name])
KTibow
  • 495
  • 6
  • 18
1

If you have any preferred editor, you can just first try opening in that editor or else open in a default editor.

ret_val = os.system("gedit %s" % file_path)
if ret_val != 0:
    webbrowswer.open(file_path)

In the above code, I am first trying to open my file in gedit editor which is my preferred editor, if the system does not have gedit installed, it just opens the file in the system's default editor.

Ysh
  • 803
  • 1
  • 9
  • 13
1

If anyone is getting an instance of internet explorer when they use import webbrowser, try declaring the full path of the specified file.

import webbrowser
import os

webbrowser.open(os.getcwd() + "/path/to/file/in/project")
#Gets the directory of the current file, then appends the path to the file

Example:

import webbrowser
import os

webbrowser.open(os.getcwd() + "/assets/ReadMe.txt")
#Will open something like "C:/Users/user/Documents/project/assets/ReadMe.txt"
DJ Martin
  • 49
  • 7