1 Star 0 Fork 0

全宗/exercise-of-python3-2022

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
get_name_size_md5.py 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
全宗 提交于 2年前 . get file name\size\md5_code
# -*- coding:utf-8 -*-
import hashlib
import os
import sys
def get_file_md5(path):
"""
获取文件内容的MD5值
:param path: 文件所在路径
:return:
"""
with open(path, 'rb') as file:
data = file.read()
diff_check = hashlib.md5()
diff_check.update(data)
md5_code = diff_check.hexdigest()
return md5_code
def get_size(path):
# 获取path目录下所有文件
for filename in os.listdir(path):
pathTmp = os.path.join(path, filename) # 获取path与filename组合后的路径
# if os.path.isdir(pathTmp): # 判断是否为目录
# get_size(pathTmp) # 是目录就继续递归查找
# elif os.path.isfile(pathTmp): # 判断是否为文件
# filesize = os.path.getsize(pathTmp) # 如果是文件,则获取相应文件的大小
if os.path.isfile(pathTmp):
filesize = os.path.getsize(pathTmp)
file_list[filename] = filesize # 将文件的大小添加到字典
return file_list
path = '/home/quanzong/factory'
file_list = {}
if __name__ == '__main__':
try:
get_size(path)
for item in file_list.keys():
path_full = os.path.join(path, item)
write_line = "{0}\t{1}\t{2}\n".format(item, file_list[item], get_file_md5(path_full))
result = "result.txt"
with open(os.path.join(path, result), 'a') as output:
output.write(write_line)
except Exception:
print("something wrong,plz check.")
sys.exit()
print("all set,go to {0},find the result.txt".format(path))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/quan-zong/exercise-of-python3-2022.git
git@gitee.com:quan-zong/exercise-of-python3-2022.git
quan-zong
exercise-of-python3-2022
exercise-of-python3-2022
master

搜索帮助