Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 638
Ben Avatar asked May 30 '11 15:05

Ben


People also ask

How do I open a text file in Notepad using Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.

Can I use Notepad as a text editor for Python?

By using Notepad and the Python interpreter, a programmer can write Python programs and execute them, or create "batch" files that can execute multiple programs, including Python scripts.

How do I open a file for reading and writing in Python?

Also if you open Python tutorial about reading and writing files you will find that: 'r+' opens the file for both reading and writing. On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'.


1 Answers

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.

like image 62
Máthé Endre-Botond Avatar answered Oct 05 '22 23:10

Máthé Endre-Botond