使用 python 脚本打开文本文件
Opening a text file with your python script
我创建了一个可以编辑文本文件内容的程序。假设我的程序看起来像这样:
from tkinter import *
from tkinter import filedialog
root = Tk()
def open_file():
file_to_open = filedialog.askopenfilename(initialdir="C:/" , filetypes=(("All files" , "*.*"),))
if file_to_open != "":
file = open(file_to_open , 'r')
content_in_file = file.read()
file.close()
text.delete('1.0' , END)
text.insert('1.0' , content_in_file)
def save_file():
path = filedialog.asksaveasfilename(initialdir="C:/" , filetypes=(("All files" , ""),))
if path != "":
file = open(path , 'w')
file.write(text.get('1.0' , 'end-1c'))
file.close()
text = Text(root , width = 65 , height = 20 , font = "consolas 14")
text.pack()
open_button = Button(root , text = "Open" , command = open_file)
open_button.pack()
save_button = Button(root , text = "Save" , command = save_file)
save_button.pack(pady=20)
mainloop()
这里的问题是,当我在我的文件资源管理器中单击一个文本文件时,它使用默认的 windows 记事本打开,而不是使用我的程序打开。
我想要的是所有文本文件都应该用我的程序打开,而不是用默认的 windows 记事本打开。
这是我所做的(按顺序):
完成以下步骤后,我尝试打开我的文本文件,但它显示:
我尝试将 python 程序转换为 exe
文件(使用 pyinstaller
),然后按照上述步骤操作,但是当我打开文本文件时出现另一个错误:
我的代码或我遵循的步骤有什么问题吗?
如果有人能指导我如何使用我的程序打开文本文件,我将不胜感激。
代码看起来不错,它只需要接受参数,当你 open-with
你在多个路径的路径上调用一些可执行文件时,第一个参数是可执行文件本身,所以如果代码是以超过 1 个参数执行,这是一条路径;这个问题是命令是 python
而不是 file.py
,修复方法是将它转换为 exe 或用 bat 调用它。
这个示例可能看起来不同,但大致相同,只是包装在 class.
中
from tkinter import filedialog
import tkinter as tk
import sys
import os
SRC_PATH = os.path.dirname(__file__)
class File_Editor(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.text = tk.Text(self, width=65, height=20, font="consolas 14")
self.text.pack()
self.open_button = tk.Button(self, text="Open", command=self.open_file)
self.open_button.pack()
self.save_button = tk.Button(self, text="Save", command=self.save_file)
self.save_button.pack(pady=20)
def open_file(self, file=None):
path = file or filedialog.askopenfilename(
initialdir=SRC_PATH, filetypes=(("All files", "*.*"),))
if os.path.exists(path) and os.path.isfile(path):
with open(path, 'r', encoding='utf-8') as file:
content_in_file = file.read()
self.text.delete('1.0', tk.END)
self.text.insert('1.0', content_in_file)
def save_file(self):
path = filedialog.asksaveasfilename(initialdir=SRC_PATH, filetypes=(("All files", ""),))
if os.path.exists(path) and os.path.isfile(path):
with open(path, 'r', encoding='utf-8') as file:
file.write(self.text.get('1.0', 'end-1c'))
if __name__ == '__main__':
app = File_Editor()
if len(sys.argv) > 1:
app.open_file(sys.argv[1])
app.mainloop()
这个 .bat 文件只是将第一个参数传递给文件;需要完整路径才能执行,它不会在你期望的目录中调用。
@echo off
python D:\Escritorio\tmp_py\temp_3.py %1
现在您只需调用 open-with File_Editor.bat
我创建了一个可以编辑文本文件内容的程序。假设我的程序看起来像这样:
from tkinter import *
from tkinter import filedialog
root = Tk()
def open_file():
file_to_open = filedialog.askopenfilename(initialdir="C:/" , filetypes=(("All files" , "*.*"),))
if file_to_open != "":
file = open(file_to_open , 'r')
content_in_file = file.read()
file.close()
text.delete('1.0' , END)
text.insert('1.0' , content_in_file)
def save_file():
path = filedialog.asksaveasfilename(initialdir="C:/" , filetypes=(("All files" , ""),))
if path != "":
file = open(path , 'w')
file.write(text.get('1.0' , 'end-1c'))
file.close()
text = Text(root , width = 65 , height = 20 , font = "consolas 14")
text.pack()
open_button = Button(root , text = "Open" , command = open_file)
open_button.pack()
save_button = Button(root , text = "Save" , command = save_file)
save_button.pack(pady=20)
mainloop()
这里的问题是,当我在我的文件资源管理器中单击一个文本文件时,它使用默认的 windows 记事本打开,而不是使用我的程序打开。
我想要的是所有文本文件都应该用我的程序打开,而不是用默认的 windows 记事本打开。
这是我所做的(按顺序):
完成以下步骤后,我尝试打开我的文本文件,但它显示:
我尝试将 python 程序转换为 exe
文件(使用 pyinstaller
),然后按照上述步骤操作,但是当我打开文本文件时出现另一个错误:
我的代码或我遵循的步骤有什么问题吗?
如果有人能指导我如何使用我的程序打开文本文件,我将不胜感激。
代码看起来不错,它只需要接受参数,当你 open-with
你在多个路径的路径上调用一些可执行文件时,第一个参数是可执行文件本身,所以如果代码是以超过 1 个参数执行,这是一条路径;这个问题是命令是 python
而不是 file.py
,修复方法是将它转换为 exe 或用 bat 调用它。
这个示例可能看起来不同,但大致相同,只是包装在 class.
中from tkinter import filedialog
import tkinter as tk
import sys
import os
SRC_PATH = os.path.dirname(__file__)
class File_Editor(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.text = tk.Text(self, width=65, height=20, font="consolas 14")
self.text.pack()
self.open_button = tk.Button(self, text="Open", command=self.open_file)
self.open_button.pack()
self.save_button = tk.Button(self, text="Save", command=self.save_file)
self.save_button.pack(pady=20)
def open_file(self, file=None):
path = file or filedialog.askopenfilename(
initialdir=SRC_PATH, filetypes=(("All files", "*.*"),))
if os.path.exists(path) and os.path.isfile(path):
with open(path, 'r', encoding='utf-8') as file:
content_in_file = file.read()
self.text.delete('1.0', tk.END)
self.text.insert('1.0', content_in_file)
def save_file(self):
path = filedialog.asksaveasfilename(initialdir=SRC_PATH, filetypes=(("All files", ""),))
if os.path.exists(path) and os.path.isfile(path):
with open(path, 'r', encoding='utf-8') as file:
file.write(self.text.get('1.0', 'end-1c'))
if __name__ == '__main__':
app = File_Editor()
if len(sys.argv) > 1:
app.open_file(sys.argv[1])
app.mainloop()
这个 .bat 文件只是将第一个参数传递给文件;需要完整路径才能执行,它不会在你期望的目录中调用。
@echo off
python D:\Escritorio\tmp_py\temp_3.py %1
现在您只需调用 open-with File_Editor.bat