From bc4e330ee7c50b6dca9a47555e428c2b3cc4712d Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 16 Oct 2023 14:27:04 +0800 Subject: [PATCH] first comit --- .gitignore | 3 +++ db.py | 32 ++++++++++++++++++++++++++++++++ getFiles.py | 17 +++++++++++++++++ getMd5.py | 23 +++++++++++++++++++++++ main.py | 26 ++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 .gitignore create mode 100644 db.py create mode 100644 getFiles.py create mode 100644 getMd5.py create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e1334f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +__pycache__/ +*.log diff --git a/db.py b/db.py new file mode 100644 index 0000000..e9547de --- /dev/null +++ b/db.py @@ -0,0 +1,32 @@ +import pymysql +from pymysql.cursors import DictCursor + +db_config = { + 'host': 'home.rogersun.cn', + 'user': 'root', + 'password': 'Sxzgx1209', + 'database': 'file_md5' +} + +db_connection = pymysql.Connect(**db_config) +db_cursor = db_connection.cursor(cursor=DictCursor) + + +def insert_data(_data): + insert_sql = "INSERT INTO files VALUES (NUAL, %s, %s, %s, %s);" + db_cursor.execute(insert_sql, _data) + db_connection.commit() + + +def get_md5_by_path(_path): + get_md5_sql = "SELECT md5 FROM files WHERE path = %s;" + db_cursor.execute(get_md5_sql, (_path,)) + r = db_cursor.fetchone() + return r + + +def is_exist(_md5): + is_exist_sql = "SELECT * FROM files WHERE md5 = %s;" + db_cursor.execute(is_exist_sql, (_md5,)) + r = db_cursor.fetchone() + return r diff --git a/getFiles.py b/getFiles.py new file mode 100644 index 0000000..aeeec84 --- /dev/null +++ b/getFiles.py @@ -0,0 +1,17 @@ +import os + + +def get_file_list(_path): + file_gen = os.walk(_path) + file_list = [] + for item in file_gen: + if len(item[2]): + for file in item[2]: + file_list.append(os.path.join(item[0], file)) + return file_list + + +if __name__ == "__main__": + result = get_file_list(r"E:\Backup\Tools\Normal\浏览器") + for r in result: + print(r) diff --git a/getMd5.py b/getMd5.py new file mode 100644 index 0000000..f72ab8c --- /dev/null +++ b/getMd5.py @@ -0,0 +1,23 @@ +import hashlib + + +def get_file_md5(file_name): + """ + 计算文件的md5 + :param file_name: + :return: + """ + m = hashlib.md5() # 创建md5对象 + with open(file_name,'rb') as fobj: + while True: + data = fobj.read(4096) + if not data: + break + m.update(data) # 更新md5对象 + + return m.hexdigest() # 返回md5对象 + + +if __name__ == "__main__": + md5_code = get_file_md5("E:\Backup\Tools\SystemISO\kms.txt") + print(md5_code) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..9c2146f --- /dev/null +++ b/main.py @@ -0,0 +1,26 @@ +from getFiles import get_file_list +from getMd5 import get_file_md5 +from db import * + + +def main_process(): + disk = "" + cate = "" + path = "" + + if disk == "" or cate == "" or path == "": + print("检查参数") + return + + file_list = get_file_list(path) + for f in file_list: + if md5 := get_md5_by_path(f): + print(f"请注意!!!文件的md5({md5})已经存在: {f}") + else: + md5 = get_file_md5(f) + insert_data((cate, disk, f, md5)) + print(f"文件的md5({md5})成功插入: {f}") + + +if __name__ == "__main__": + main_process()