python3实现指定目录下文件sha256及文件大小统计
作者:fengbingchun 时间:2023-11-26 15:37:00
有时会统计某个目录下有哪些文件,每个文件的sha256及文件大小等相关信息,这里用python3写了个脚本用来实现此功能,此脚本可跨平台,同时支持windows和linux,脚本(get_dir_file_info.py)内容如下:
import os
import sys
import hashlib
def Usage():
''' usage description '''
num = len(sys.argv)
if num != 3:
print("Error: please input two parameters")
print("for example: {} path_name save_file_name".format(sys.argv[0]))
sys.exit(1)
def GetFilesList():
''' get file list '''
input_path_name = sys.argv[1]
result = list()
for dirpath, dirnames, filenames in os.walk(input_path_name, followlinks=True):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
result.append(file_path)
result.sort()
return result
def CalcFileSha256(filname):
''' calculate file sha256 '''
with open(filname, "rb") as f:
sha256obj = hashlib.sha256()
sha256obj.update(f.read())
hash_value = sha256obj.hexdigest()
return hash_value
def CalcFileSize(filename):
''' calculate file size '''
return os.stat(filename).st_size
def GetFileContent():
''' get file contnet '''
files_list = GetFilesList()
result = list()
for f in files_list:
hash = CalcFileSha256(f)
size = CalcFileSize(f)
file_name = os.path.basename(os.path.realpath(f))
path_name = os.path.dirname(os.path.realpath(f))
dictionary = {"path": path_name, "filename": file_name, "sha256": hash, "size": size}
#print("result: {}".format(dictionary))
result.append(dictionary)
return result
def WriteToFile(contents):
''' write content to the specified file '''
fp = open(sys.argv[2], "w")
for content in contents:
#print("content:", content)
str0 = str(content)
str1 = str0.replace("\\\\", "/")
fp.write(str1)
fp.write("\n")
fp.close()
def ReplaceStr(src_str, new_str):
''' replace source string with new string '''
contents = list()
fp = open(sys.argv[2], "r")
line = fp.readline()
while line:
contents.append(line)
line = fp.readline()
fp.close()
fp = open(sys.argv[2], "w")
for content in contents:
str0 = content.replace(src_str, new_str)
fp.write(str0)
fp.close()
def main():
Usage()
WriteToFile(GetFileContent())
ReplaceStr(" ", "")
ReplaceStr("'", "\"")
if __name__ == "__main__":
main()
执行操作如下:
执行结果及生成的prj_file_list.txt内容如下:
GitHub:Python_Test
来源:https://blog.csdn.net/fengbingchun/article/details/86665247
标签:python3,sha256,文件统计
0
投稿
猜你喜欢
Python实现CAN报文转换工具教程
2022-06-13 02:34:06
Python获取"3年前的今天"的日期时间问题
2022-02-01 12:12:13
Python+logging输出到屏幕将log日志写入文件
2023-07-19 05:29:21
pandas数据集的端到端处理
2023-01-20 04:48:43
CSS代码实现下划线样式的输入框效果
2010-03-16 12:42:00
浅谈django 模型类使用save()方法的好处与注意事项
2021-05-17 10:43:59
asp常用的SQL命令操作
2007-09-29 12:21:00
利用tkinter改变下拉列表(Combobox)的选项值
2023-07-20 03:10:37
python 读取Linux服务器上的文件方法
2023-03-31 23:54:19
如何决定是否将登录内容保存到Cookie里?
2009-12-16 18:54:00
python实现数据预处理之填充缺失值的示例
2022-01-24 19:44:30
python 使用第三方库requests-toolbelt 上传文件流的示例
2021-05-13 05:48:31
Python深度学习albumentations数据增强库
2023-02-14 20:00:18
python求解水仙花数的方法
2023-10-08 12:07:28
python学生信息管理系统实现代码
2023-10-31 06:18:10
详解JavaScript中的this指向问题
2023-08-23 00:52:03
python游戏测试工具自动化遍历游戏中所有关卡
2021-10-05 13:03:20
Django 项目重命名的实现步骤解析
2021-01-13 14:05:49
Python实现连接两个无规则列表后删除重复元素并升序排序的方法
2021-05-23 10:35:54
Python OpenCV之常用滤波器使用详解
2023-02-07 22:18:25