Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
程序员晚枫
python-office
提交
4fe6186b
P
python-office
项目概览
程序员晚枫
/
python-office
通知
272
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
python-office
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4fe6186b
编写于
5月 25, 2022
作者:
S
sustnf
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add md5_verify&file
上级
05c1e7ad
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
159 addition
and
0 deletion
+159
-0
contributors/sustnf/file.py
contributors/sustnf/file.py
+70
-0
contributors/sustnf/md5_verify.py
contributors/sustnf/md5_verify.py
+89
-0
未找到文件。
contributors/sustnf/file.py
0 → 100644
浏览文件 @
4fe6186b
# -*- coding:utf-8 -*-
import
os
import
shutil
import
sys
from
typing
import
List
class
HiddenPrints
:
def
__enter__
(
self
):
self
.
_original_stdout
=
sys
.
stdout
sys
.
stdout
=
open
(
os
.
devnull
,
"w"
)
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
sys
.
stdout
.
close
()
sys
.
stdout
=
self
.
_original_stdout
# 判断文件夹中是否有超过固定大小的文件
# 可对超过具体大小的文件做删除,移动等操作,后续优化
# folder:文件夹
# size:大小(M)
def
screen_file
(
folder
,
size
:
int
):
size
=
size
*
1024
*
1024
res
=
[]
reslist
=
[]
for
path
,
dirs
,
file_names
in
os
.
walk
(
folder
,
topdown
=
True
):
for
filename
in
file_names
:
res
.
append
(
os
.
path
.
join
(
path
,
filename
))
for
r
in
res
:
if
os
.
path
.
getsize
(
r
)
>
size
:
reslist
.
append
(
r
)
if
len
(
reslist
)
==
0
:
print
(
"没有超过%sM大小的文件"
%
(
size
/
1024
/
1024
))
else
:
print
(
"有超过%sM大小的文件"
%
(
size
/
1024
/
1024
))
for
r
in
reslist
:
print
(
r
)
# screen_file("E:\\",200)
# 判断文件夹中指定后缀的文件(后缀可多写List类型)['.exe','.txt','.md']
def
screen_suffix
(
folder
,
suffix
:
List
):
reslist
=
[]
for
s
in
range
(
len
(
suffix
)):
# with HiddenPrints():
one_suffix
(
folder
,
suffix
[
s
],
reslist
)
for
r
in
reslist
:
print
(
r
)
# 判断文件夹中指定后缀的文件
def
one_suffix
(
folder
,
suf
,
res
=
None
):
res
=
[]
datanames
=
os
.
listdir
(
folder
)
for
dataname
in
datanames
:
t
=
os
.
path
.
join
(
folder
,
dataname
)
if
os
.
path
.
isfile
(
t
):
if
dataname
.
endswith
(
suf
):
res
.
append
(
os
.
path
.
join
(
folder
,
dataname
))
elif
os
.
path
.
isdir
(
t
):
one_suffix
(
t
,
suf
,
res
)
for
r
in
res
:
print
(
r
)
# screen_suffix("E:\项目(gitee)", ['.txt','.md','.js'])
# one_suffix("E:\项目(gitee)",".md")
contributors/sustnf/md5_verify.py
0 → 100644
浏览文件 @
4fe6186b
# -*- coding:utf-8 -*-
import
hashlib
import
os
# 文件md5校验
# source_file:文件1
# target_file:文件2
def
file_compare
(
source_file
,
target_file
):
s
=
open
(
source_file
,
"br"
)
t
=
open
(
target_file
,
"br"
)
md5_source_file
=
hashlib
.
md5
(
s
.
read
()).
hexdigest
()
md5_target_file
=
hashlib
.
md5
(
t
.
read
()).
hexdigest
()
if
md5_source_file
==
md5_target_file
:
print
(
"文件%s和文件%s
\t
md5值相同"
%
(
source_file
,
target_file
))
else
:
print
(
"文件%s和文件%s
\t
md5值不相同"
%
(
source_file
,
target_file
))
# 文件夹(子目录)md5校验。生成.csv文档,用于查看对比结果(使用excel打开即可)
# 目前校验相同和不相同都在同一文件下,后续优化
# source_folder:文件夹1
# target_folder:文件夹2
# result.csv生成在执行目录下
def
folder_compare
(
source_folder
,
target_folder
):
output_file
=
os
.
getcwd
()
+
"
\\
result.csv"
# 判断是否已指定的路径分隔符结尾
if
source_folder
.
endswith
(
os
.
path
.
sep
)
and
len
(
source_folder
)
>
1
:
source_folder
=
source_folder
[:
-
1
]
if
target_folder
.
endswith
(
os
.
path
.
sep
)
and
len
(
target_folder
)
>
1
:
target_folder
=
target_folder
[:
-
1
]
# 遍历文件或者目录
t
=
os
.
walk
(
source_folder
,
topdown
=
True
)
# os.walk返回一个三元组
# path:当前正在遍历的这个文件夹的本身的地址,
# dirs:是一个list,内容是该文件夹中所有的目录的名字(不包括子目录),
# file_names:是一个list,内容是该文件夹中所有的文件(不包括子目录),
# topdown参数为真,walk会遍历source_folder文件夹,与source_folder文件夹中每一个子目录
output_file
=
open
(
output_file
,
"w+"
)
for
path
,
dirs
,
file_names
in
t
:
for
filename
in
file_names
:
source_file
=
os
.
path
.
join
(
path
,
filename
)
# 链接路径
target_file
=
source_file
.
replace
(
source_folder
,
target_folder
)
f
=
open
(
source_file
,
"br"
)
# print(source_file)
# md5值
md5_source_file
=
hashlib
.
md5
(
f
.
read
()).
hexdigest
()
f
.
close
()
if
os
.
path
.
exists
(
target_file
):
f
=
open
(
target_file
,
"br"
)
md5_target_file
=
hashlib
.
md5
(
f
.
read
()).
hexdigest
()
f
.
close
()
if
md5_source_file
==
md5_target_file
:
try
:
output_file
.
write
(
"%s,%s,%s,%s,%s
\n
"
%
(
source_file
,
md5_source_file
,
target_file
,
md5_target_file
,
"true"
)
)
except
:
output_file
.
write
(
",%s,,%s,%s
\n
"
%
(
""
,
md5_source_file
,
""
,
md5_target_file
,
"true"
)
)
print
(
"%s 和 %s 相等,记录在校验结果中"
%
(
source_file
,
target_file
))
else
:
try
:
output_file
.
write
(
"%s,%s,%s,%s,%s
\n
"
%
(
source_file
,
md5_source_file
,
target_file
,
md5_target_file
,
"false"
)
)
except
:
output_file
.
write
(
",%s,,%s,%s
\n
"
%
(
""
,
md5_source_file
,
""
,
md5_target_file
,
"false"
)
)
print
(
"%s 和 %s 不相等,记录在校验结果中"
%
(
source_file
,
target_file
))
else
:
try
:
output_file
.
write
(
"%s,%s,%s,%s,%s
\n
"
%
(
source_file
,
md5_source_file
,
"not exists"
,
""
,
"false"
)
)
except
:
output_file
.
write
(
",%s,,%s,%s
\n
"
%
(
""
,
md5_source_file
,
""
,
"not exists"
,
"false"
)
)
print
(
"%s不存在target(目标文件夹),记录在校验结果中"
%
source_file
)
output_file
.
close
()
print
(
"校验完成,结果请查看%s文件"
%
(
output_file
.
name
))
# file_compare("D:\\blog\\bin\\yarn","C:\\Users\\DUZUN\\Desktop\\bin\\yarn")
# folder_compare("D:\\blog\\bin","C:\\Users\\DUZUN\\Desktop\\bin")
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录