首页 > 编程语言 >python写的文件比对脚本:

python写的文件比对脚本:

时间:2023-09-08 17:13:13浏览次数:40  
标签:脚本 __ 文件 python text window tk row

上代码:

# -*- coding: utf-8 -*- 
import difflib,webbrowser
import os
import tkinter as tk
from tkinter import filedialog,messagebox

def readfile(filename):
try:
with open(filename,'r+',encoding='utf-8') as f:
text=f.read().splitlines() #结果为每行的列表
return text
except IOError as error:
messagebox.showerror('警告',filename+'读取失败'+str(error))

def savediff(file1,file2):
lines1=readfile(file1)
lines2=readfile(file2)
d=difflib.HtmlDiff()
with open(htmlfile,'w',encoding='UTF-8') as f:
res=d.make_file(lines1,lines2)
f.write(res)

def getfile(filepath):
dirs=os.path.dirname(os.path.abspath(__file__))
path=filedialog.askopenfilename(title=u'选择文件',initialdir=dirs)
filepath.set(path)

def compare():
file1=path1.get()
file2=path2.get()
savediff(file1,file2)
webbrowser.open(htmlfile) #打开html

if __name__=='__main__':
window = tk.Tk()
window.title('文件比对小工具')
window.geometry('500x400') # 这里的乘是小x
window.wm_attributes('-topmost',True)
path1=tk.StringVar()
path2=tk.StringVar()
htmlfile='比对结果.html'
tk.Button(window,text='打开文件一',command=lambda:getfile(path1)).grid(row=0,column=0,pady=2)
tk.Entry(window,textvariable=path1,width=40).grid(row=0,column=1)
tk.Button(window,text='打开文件二',command=lambda:getfile(path2)).grid(row=1,column=0,pady=2)
tk.Entry(window,textvariable=path2,width=40).grid(row=1,column=1)
tk.Button(window,text="执行文件比对",command=compare).grid(row=2,column=0,pady=5)
window.mainloop()



实现效果如下:
然后选择文件路径,执行文件比对,会跳出来网页显示两者文件间的区别

 

标签:脚本,__,文件,python,text,window,tk,row
From: https://www.cnblogs.com/cherishthepresent/p/17688073.html

相关文章

  • python内置定时任务
    目录python内置定时任务whileTrue+sleepTimeloopTimerschedpython内置定时任务whileTrue+sleepimportdatetimedeftask_run(*args,**kwargs):print(f"耗时操作....{datetime.datetime.now().strftime('%Y%m%d_%H:%M:%S')}")defcron_and_sleep():......
  • 在线问诊 Python、FastAPI、Neo4j — 创建节点
    目录前提条件创建节点Demo准备数据在线问诊Python、FastAPI、Neo4j—创建节点Neo4j节点的标签可以理解为Java中的实体。根据常规流程:首先有什么症状,做哪些对应的检查,根据检查诊断什么疾病,需要用什么药物治疗,服药期间要注意哪些饮食,需要做哪些运行在线问诊大概创建:症状......
  • 打开vhdx格式文件
    Windows,打开磁盘管理器,AttachVHDLinux-Ubuntusudoaptinstalllibguestfs-tools查看vhdx分区:sudovirt-list-filesystems/vhdx-filesudoguestmount-a/vhdx-file-m/dev/sda1-r/path/mount-oallow_other注:-r:readonlyallow_other:允许其他用户使用......
  • Python中的异常处理机制
    finally语句是Python中异常处理机制的一部分,它总是会被执行,无论是否发生异常。finally语句通常用于释放资源或执行清理操作。下面是一个简单的例子:try:#代码段1passexceptExceptionType:#代码段2passelse:#代码段3passfinally:#代码段4......
  • python查看变量类型
    在python中有两种方式来查看变量类型,一种是直接使用tpye(object)函数直接输出变量类型,另一种是使用isinstance(x,A_tuple)来判断变量是否属于某一类型,输出结果为True,则属于该类型,反之则不属于。type(object):使用type(object)函数查看数据的类型;alist=[1,2,3,4,5]print(......
  • python flask有像Spring AOP一样 捕获记录操作过程请求和返回
    在PythonFlask中,你可以使用装饰器(decorators)或中间件(middlewares)来实现类似SpringAOP的日志记录功能,以捕获和记录操作过程的请求和返回。一种常见的方法是使用装饰器来包装路由处理函数,在函数执行前后记录相关信息:```pythonfromfunctoolsimportwrapsfromflaskimport......
  • python3 postgreSQL 依赖问题
    unabletoexecute'gcc':NosuchfileordirectoryItappearsyouaremissingsomeprerequisitetobuildthepackagefromsource.Youmayinstallabinarypackagebyinstalling'psycopg2-binary'fromPyPI.Ifyouwantto......
  • Python 网页爬虫原理及代理 IP 使用
    一、Python网页爬虫原理Python是一种高效的编程语言,在Web开发和数据分析领域广受欢迎。Python的优秀模块使其更加适合大规模数据处理和Web服务的编程。网络爬虫是Python开发者最常用的工具之一。网络爬虫(WebCrawler)是一种自动化程序,可以模拟人类浏览器的行为,自动在互联网......
  • Web阶段:第十五章:文件上传&下载
    文件的上传介绍文件的上传和下载功能是很多系统中非常常见的功能。非常的重要。文件的上传1、首先需要一个form表单2、然后在表单内有inputtype=”file”3、提交的方式必须是method=”POST”4、enctype="multipart/form-data"编码类型必须是多段的表单数据enctype属性有两种值:......
  • 43道Python经典案例题(有答案)
    1.有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?forxinrange(0,5):foryinrange(0,5):forzinrange(0,5):ifx!=yandy!=zandz!=x:print(x,y,z)复制2.题目:企业发放的奖金根据利润提成......