myFile.py
import os
'''
获取dirpath文件夹下所有txt文件的路径
'''
def getTxtPaths(dirpath):
paths = [ ]
filenames = os.listdir(dirpath)
for filename in filenames :
filepath = os.path.join(dirpath,filename)
if os.path.isfile(filepath) and filename.endswith('.txt'):
paths.append(filepath)
return paths
'''
读取一个txt文件的内容,filepath是文件路径
'''
def readOneTxt(filepath):
try:
file = open(filepath, 'r')
text = file.read()
file.close()
return text
except:
return ''
'''
将多个txt文件(原始文件)合并成一个txt文件(结果文件)
srcPaths是一个列表,存放了所有原始文件的路径
destPath是一个字符串,表示结果文件的路径
'''
def mergeTxts(srcPaths, destPath):
destFile = open(destPath, 'w')
for srcPath in srcPaths:
text = readOneTxt(srcPath)
if text != '':
destFile.write(text)
destFile.write('\n'*2+'-'*50+'\n'*2)
destFile.close()
return os.path.realpath(destPath)
if __name__ == '__main__':
srcPaths = getTxtPaths('红楼梦') #获取“红楼梦”文件夹下所有txt文件的路径
mergeTxts(srcPaths, 'result.txt') #对文本文件进行合并
print(readOneTxt('result.txt')) #读取并打印结果文件
myProcess.py
from subprocess import Popen
import psutil
import os
def openOneTxt(path):
if os.path.exists(path) and path.endswith('.txt'):
Popen( ['notepad.exe', path] )
def openManyTxt(paths):
for path in paths:
openOneTxt(path)
def closeAllTxt():
for p in psutil.process_iter():
if p.name().lower()=='notepad.exe':
p.terminate()
if __name__ == '__main__':
import time
paths=['红楼梦\\第001回.txt',
'红楼梦\\第002回.txt',
'红楼梦\\第003回.txt',
'红楼梦\\第004回.txt']
openManyTxt(paths)
time.sleep(3)
closeAllTxt()
myTool.py
import tkinter as tk
from tkinter import scrolledtext
from tkinter import filedialog
import myFile
import myProcess
#“添加文件”按钮的消息响应函数
def clickButton_addFile():
filePaths=filedialog.askopenfilenames(title='选择文件',filetypes=[('txt', '.txt')])
for filePath in filePaths:
textbox_txtPath.insert(tk.END,filePath+'\n')
textbox_txtPath.see(tk.END)
#“添加文件夹”按钮的消息响应函数
def clickButton_addDir():
dirpath=filedialog.askdirectory(title='选择文件夹')
filePaths = myFile.getTxtPaths(dirpath)
for filePath in filePaths:
textbox_txtPath.insert(tk.END,filePath+'\n')
textbox_txtPath.see(tk.END)
#“开始合并”按钮的消息响应函数
def clickButton_merge():
tmp = textbox_txtPath.get(0.0, tk.END)
filePaths = tmp.split('\n')
destPath = myFile.mergeTxts(filePaths, 'result.txt')
textbox_resultPath.configure(state=tk.NORMAL)
textbox_resultPath.delete(0.0, tk.END)
textbox_resultPath.insert(0.0, destPath)
textbox_resultPath.configure(state=tk.DISABLED)
#“打开结果”按钮的消息响应函数
def clickButton_openResult():
destFile = textbox_resultPath.get(0.0, tk.END).strip()
myProcess.openOneTxt(destFile)
#“打开原始文件”按钮的消息响应函数
def clickButton_openAll():
tmp = textbox_txtPath.get(0.0, tk.END)
filePaths = tmp.split('\n')
myProcess.openManyTxt(filePaths)
#“关闭所有文件”按钮的消息响应函数
def clickButton_closeAll():
myProcess.closeAllTxt()
#主窗口
window = tk.Tk()
window.title('文件合并工具')
window.geometry('440x600+300+200')
window.resizable(False, False)
#“添加文件”按钮
button_addFile=tk.Button(window,text='添加文件',font=('楷体',12), command=clickButton_addFile)
button_addFile.place(x=20, y=20, width=120, height=40)
#“添加文件夹”按钮
button_addDir=tk.Button(window,text='添加文件夹',font=('楷体',12), command=clickButton_addDir)
button_addDir.place(x=160, y=20, width=120, height=40)
#“开始合并”按钮
button_merge=tk.Button(window,text='开始合并',font=('楷体',12), bg='orange', command=clickButton_merge)
button_merge.place(x=300, y=20, width=120, height=40)
#用于展示所选文件对应路径的文本框(可滚动)
textbox_txtPath=scrolledtext.ScrolledText(window)
textbox_txtPath.place(x=20,y=70,width=400,height=400)
#“生成结果”标签
label=tk.Label(window, text='生成结果:', font=('楷体',12))
label.place(x=20,y=490,width=100,height=40)
#用于展示生成结果路径的文本框
textbox_resultPath=tk.Text(window)
textbox_resultPath.place(x=120,y=490,width=300,height=40)
textbox_resultPath.configure(state=tk.DISABLED)#不可修改
#“打开结果”按钮
button_openResult=tk.Button(window,text='打开结果',font=('楷体',12), bg='orange', command=clickButton_openResult)
button_openResult.place(x=20, y=540, width=120, height=40)
#“打开所有”按钮
button_openAll=tk.Button(window,text='打开原始文件',font=('楷体',12), command=clickButton_openAll)
button_openAll.place(x=160, y=540, width=120, height=40)
#“关闭所有”按钮
button_closeAll=tk.Button(window,text='关闭所有文件',font=('楷体',12), command=clickButton_closeAll)
button_closeAll.place(x=300, y=540, width=120, height=40)
#显示图形化界面
window.mainloop()