我被困住了。我想获取用户指定的Windows目录,并在表中列出该目录中的每个文件,以及路径,文件名,文件大小,上次修改时间和MD5哈希值。为了我的一生,我不知道该如何将其分解为单个文件。它只会完成整个路径。我知道路径变量需要输入到目录中的各个文件中,但是我不知道该怎么做。
如何相应地创建表,并添加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)
答案 0 :(得分:1)
此解决方案符合您的要求吗?使用内置的pathlib:
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 |
+-------------------+------------------+-----------+----------------------------+----------------------------------+