python3实现指定目录下文件sha256及文件大小统计

Ursula ·
更新时间:2024-09-21
· 699 次阅读

有时会统计某个目录下有哪些文件,每个文件的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

您可能感兴趣的文章:Python读取系统文件夹内所有文件并统计数量的方法Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】Python实现统计文本文件字数的方法Python统计文件中去重后uuid个数的方法Python实现对excel文件列表值进行统计的方法python统计文本文件内单词数量的方法Python3读取UTF-8文件及统计文件行数的方法python 远程统计文件代码分享python脚本实现统计日志文件中的ip访问次数代码分享使用python统计文件行数示例分享



sha sha256 Python3 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号