赞
踩
目录
可以考虑以下几个可能的应用:
基于这些应用,我为你举了以下几个具体的场景:
- 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 subdirectories
- if os.path.isdir(file):
- continue
- # Get the full file path
- file_path = os.path.abspath(file)
- # Generate the md5 hash of the file content
- md5_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 table
- cursor.execute("INSERT INTO filemd (filepath, md5) VALUES (?, ?)", (file_path, md5_hex))
-
- # Commit and close the connection
- conn.commit()
- conn.close()
这段代码的主要功能是遍历当前文件夹下的所有文件,计算每个文件的MD5码,并将文件路径和MD5码存储在Access数据库中。具体来说,这段代码做了以下几件事:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。