| | | | | | | | | import sqlite3 | | import os | | import datetime | | import time | | import csv | | | | | | def getFileInfo(path): | | absname = '' | | timestamp = 0 | | mdate = '' | | for rootDir, baseDir, files in os.walk(path): | | for file in files: | | absname = os.path.join(rootDir, file) | | timestamp = time.localtime(os.path.getmtime(absname)) | | yield (None, | | file, | | absname, | | datetime.datetime.strptime( | | time.strftime('%Y%m%d%H%M%S',timestamp),'%Y%m%d%H%M%S'), | | os.path.getsize(absname) | | ) | | | | | | conn = sqlite3.connect('fileInfo.db') | | | | | | cur = conn.cursor() | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | cur.execute("select * from fileInfo") | | csvHeader = ['序号', '文件名', '文件绝对路径', '文件最后修改日期', '文件大小(单位:KB)'] | | with open('fileInfo.csv', 'w', encoding='utf-8', newline='') as f: | | csvf = csv.writer(f) | | csvf.writerow(csvHeader) | | for n in cur.fetchall(): | | csvf.writerow(n) | | | | | | cur.close() | | | | conn.commit() | | | | conn.close() | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | COPY |
utf-8 转 gb18030 编码转换 | | | | | | | | | | | | | with open('fileInfo.csv', encoding='utf-8') as f: | | with open('fileInfoANSI.csv', 'wb') as af: | | for line in f: | | af.write(line.encode('GB18030', errors='ignore'))COPY |
|