好友
阅读权限 10
听众
最后登录 1970-1-1
度娘:链接: https://pan.baidu.com/s/1foD-1-D8uJS6gYxS6Jsmng?pwd=hzhz 提取码: hzhz
import os
import tkinter as tk
from tkinter import filedialog, messagebox
def merge_txt_files (input_dir , output_file ):
"""合并指定目录下的所有txt文件到一个输出文件"""
# 确保输出目录存在
os.makedirs(os.path.dirname(output_file), exist_ok = True )
# 获取所有txt文件并按名称排序
txt_files = sorted ([f for f in os.listdir(input_dir) if f.endswith('.txt' )])
if not txt_files:
messagebox.showwarning("警告" , "所选目录下没有找到txt文件!" )
return
# 合并文件内容
with open (output_file, 'w' , encoding = 'utf-8' ) as outfile:
for txt_file in txt_files:
file_path = os.path.join(input_dir, txt_file)
try :
# 尝试多种编码方式读取文件
encodings = ['utf-8' , 'gbk' , 'gb2312' , 'ansi' ]
content = None
for encoding in encodings:
try :
with open (file_path, 'r' , encoding = encoding) as infile:
content = infile.read()
break
except UnicodeDecodeError :
continue
if content is None :
raise UnicodeDecodeError (f "无法使用已知编码方式读取文件 { txt_file} " )
# 写入文件名作为标题,使用等号装饰
outfile.write('=' * 20 + ' \n ' )
outfile.write(f "《 { os.path.splitext(txt_file)[0 ]} 》 \n " )
outfile.write('=' * 20 + ' \n\n ' )
outfile.write(content)
outfile.write(' \n\n ' )
except Exception as e:
messagebox.showerror("错误" , f "处理文件 { txt_file} 时出错: { str (e)} " )
class MergeTxtGUI :
def __init__ (self ):
self .window = tk.Tk()
self .window.title("TXT文件合并工具" )
self .window.geometry("600x350" )
# 添加说明标签
self .info_label = tk.Label(self .window,
text = "本工具用于合并指定目录下的所有TXT文件,并在合并时添加文件名作为标题" ,
wraplength = 550 )
self .info_label.pack(pady = 10 )
# 创建输入目录选择部分
self .input_frame = tk.Frame(self .window)
self .input_frame.pack(pady = 20 )
self .input_label = tk.Label(self .input_frame, text = "输入目录:" )
self .input_label.pack(side = tk.LEFT)
self .input_entry = tk.Entry(self .input_frame, width = 50 )
self .input_entry.pack(side = tk.LEFT, padx = 5 )
self .input_button = tk.Button(self .input_frame, text = "浏览" , command = self .select_input_dir)
self .input_button.pack(side = tk.LEFT)
# 创建输出文件选择部分
self .output_frame = tk.Frame(self .window)
self .output_frame.pack(pady = 20 )
self .output_label = tk.Label(self .output_frame, text = "输出文件:" )
self .output_label.pack(side = tk.LEFT)
self .output_entry = tk.Entry(self .output_frame, width = 50 )
self .output_entry.pack(side = tk.LEFT, padx = 5 )
self .output_button = tk.Button(self .output_frame, text = "浏览" , command = self .select_output_file)
self .output_button.pack(side = tk.LEFT)
# 创建合并按钮
self .merge_button = tk.Button(self .window, text = "开始合并" ,
command = self .start_merge,
width = 20 ,
height = 2 )
self .merge_button.pack(pady = 20 )
def select_input_dir (self ):
directory = filedialog.askdirectory(title = "选择包含TXT文件的目录" )
if directory:
self .input_entry.delete(0 , tk.END)
self .input_entry.insert(0 , directory)
def select_output_file (self ):
file_path = filedialog.asksaveasfilename(
title = "选择保存位置" ,
defaultextension = ".txt" ,
filetypes = [("Text files" , "*.txt" )]
)
if file_path:
self .output_entry.delete(0 , tk.END)
self .output_entry.insert(0 , file_path)
def start_merge (self ):
input_dir = self .input_entry.get()
output_file = self .output_entry.get()
if not input_dir or not output_file:
messagebox.showerror("错误" , "请选择输入目录和输出文件路径!" )
return
try :
merge_txt_files(input_dir, output_file)
messagebox.showinfo("成功" , f "合并完成! \n 输出文件: { output_file} " )
except Exception as e:
messagebox.showerror("错误" , f "合并过程出错: { str (e)} " )
if __name__ == "__main__" :
app = MergeTxtGUI()
app.window.mainloop()
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。