first comit

main
RogerWork 1 year ago
commit bc4e330ee7
  1. 3
      .gitignore
  2. 32
      db.py
  3. 17
      getFiles.py
  4. 23
      getMd5.py
  5. 26
      main.py

3
.gitignore vendored

@ -0,0 +1,3 @@
.idea/
__pycache__/
*.log

32
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

@ -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)

@ -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)

@ -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()
Loading…
Cancel
Save