import webbrowser
webbrowser.open("file.txt")
尽管它的名称是,它将在记事本,gedit等中打开。从未尝试过,但据说可以奏效。
替代方法是使用
osCommandString = "notepad.exe file.txt"
os.system(osCommandString)
或作为子流程:
import subprocess as sp
programName = "notepad.exe"
fileName = "file.txt"
sp.Popen([programName, fileName])
但是在这两种情况下,您都需要先找到给定操作系统的本机文本编辑器。