我被困住了。我想获取用户指定的 Windows 目录,并在表上列出该目录中的每个文件,其中包含路径、文件名、文件大小、上次修改时间和 MD5 哈希。对于我的一生,我不知道如何将其分解为单个文件;它只执行整个路径。我知道 path 变量需要转到目录中的各种文件,但我不知道该怎么做。
如何相应地创建表,并添加 MD5 哈希列。上次修改时间应该是人类可读的格式,而不是 UNIX 时间戳。
#import libraries
import os
import time
import datetime
import logging
import hashlib
from prettytable import PrettyTable
import glob
#user input
path = input ("Please enter directory: ")
verbose = input ("Please enter yes/no for verbose: ")
print ("===============================================")
#processing input
if os.path.exists(path):
print("Processing directory: ", (path))
else:
print("Invalid directory.")
exit()
if (verbose) == ("yes"):
print("Verbose selected")
elif (verbose) == ("no"):
print("Verbose not selected")
else:
print("Invalid input")
print ("===============================================")
#process directory
directory = glob.glob(path)
filename = os.path.basename(path)
size = os.path.getsize(path)
modified = os.path.getmtime(path)
#output in to table
report = PrettyTable()
column_names = ['Path', 'File Name', 'File Size', 'Last Modified Time', 'MD5 Hash']
report.add_column(column_names[0], [directory])
report.add_column(column_names[1], [filename])
report.add_column(column_names[2], [size])
report.add_column(column_names[3], [modified])
report.sortby = 'File Size'
print (report)
此解决方案是否符合您的要求?使用内置的路径库:
from pathlib import Path
from datetime import datetime
import hashlib
#...Your code getting path here...
directory = Path(path)
paths = []
filename = []
size = []
hashes = []
modified = []
files = list(directory.glob('**/*.*'))
for file in files:
paths.append(file.parents[0])
filename.append(file.parts[-1])
size.append(file.stat().st_size)
modified.append(datetime.fromtimestamp(file.stat().st_mtime))
with open(file) as f:
hashes.append(hashlib.md5(f.read().encode()).hexdigest())
#output in to table
report = PrettyTable()
column_names = ['Path', 'File Name', 'File Size', 'Last Modified Time', 'MD5 Hash']
report.add_column(column_names[0], paths)
report.add_column(column_names[1], filename)
report.add_column(column_names[2], size)
report.add_column(column_names[3], modified)
report.add_column(column_names[4], hashes)
report.sortby = 'File Size'
print(report)
输出:
+-------------------+------------------+-----------+----------------------------+----------------------------------+
| Path | File Name | File Size | Last Modified Time | MD5 Hash |
+-------------------+------------------+-----------+----------------------------+----------------------------------+
| C:...New folder | 1 - Copy (2).txt | 0 | 2019-12-05 15:35:31.562420 | d41d8cd98f00b204e9800998ecf8427e |
| C:...New folder | 1 - Copy (3).txt | 0 | 2019-12-05 15:35:31.562420 | d41d8cd98f00b204e9800998ecf8427e |
| C:...New folder | 1 - Copy.txt | 0 | 2019-12-05 15:35:31.562420 | d41d8cd98f00b204e9800998ecf8427e |
| C:...New folder | 1.txt | 0 | 2019-12-05 15:35:31.562420 | d41d8cd98f00b204e9800998ecf8427e |
+-------------------+------------------+-----------+----------------------------+----------------------------------+