栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python shutil模块文件及文件目录的操作;python DiffLib 文本内容对比

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python shutil模块文件及文件目录的操作;python DiffLib 文本内容对比

1、python shutil模块文件及文件目录的操作

参考:https://blog.csdn.net/weixin_42444693/article/details/104475705
https://www.cnblogs.com/caibao666/p/6433864.html

递归删除文件

shutil.rmtree('folder1')

拷贝文件

shutil.copyfile('f1.log', 'f2.log')

压缩文件
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录

import shutil

ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
2、python DiffLib 文本内容对比

参考:https://blog.csdn.net/wumingxiaoyao/article/details/115444548

***SequenceMatcher 找出相同 与difflib.Differ 找出不同

import difflib
from difflib import SequenceMatcher

l1 = "Hello, Welcome to CoderzColumn."
l2 = "Welcome to CoderzColumn, Have a Great Learning Day."
seq_mat_with_junk = SequenceMatcher(isjunk=lambda x: x in [",", "."], a=l1, b=l2, autojunk=True)
seq_mat_without_junk = SequenceMatcher(a=l1, b=l2, autojunk=True)
print("similarity with junk:{}".format(seq_mat_with_junk.ratio()))
print("similarity without junk:{}".format(seq_mat_without_junk.ratio()))

# output
similarity with junk:0.5609756097560976
similarity without junk:0.5853658536585366

import difflib
from difflib import Differ
str1 = "I would like to order a pepperoni pizza.n And you?"
str2 = "I would like to order a veggie burger.n And you?"
str1_lines = str1.splitlines()
str2_lines = str2.splitlines()
d = difflib.Differ()
diff = d.compare(str1_lines, str2_lines)
for lines in diff:
    print(lines)

***difflib.HtmlDiff 网页显示不同的

import difflib
from IPython import display
a = open("original.txt", "r").readlines()
b = open("modified.txt", "r").readlines()
difference = difflib.HtmlDiff(tabsize=2)
with open("compare.html", "w") as fp:
    html = difference.make_file(fromlines=a, tolines=b, fromdesc="Original", todesc="Modified")
    fp.write(html)
display.HTML(open("compare.html", "r").read())

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号