目录
简介:
源代码:
源代码说明:
效果如下所示:
可以考虑以下几个可能的应用:
基于这些应用,我为你举了以下几个具体的场景:
import os
import hashlib
import pyodbc# Connect to the Access database
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=./stocks.accdb;')
# conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=stocks.accdb;')
cursor = conn.cursor()# Iterate over all files in the current folder
for file in os.listdir("."):# Skip subdirectoriesif os.path.isdir(file):continue# Get the full file pathfile_path = os.path.abspath(file)# Generate the md5 hash of the file contentmd5_hash = hashlib.md5()with open(file_path, "rb") as f:for chunk in iter(lambda: f.read(4096), b""):md5_hash.update(chunk)md5_hex = md5_hash.hexdigest()# Insert the file path and md5 hash into the database tablecursor.execute("INSERT INTO filemd (filepath, md5) VALUES (?, ?)", (file_path, md5_hex))# Commit and close the connection
conn.commit()
conn.close()
这段代码的主要功能是遍历当前文件夹下的所有文件,计算每个文件的MD5码,并将文件路径和MD5码存储在Access数据库中。具体来说,这段代码做了以下几件事: