【发布时间】:2021-06-13 09:32:41
【问题描述】:
这是我认为可能发生错误的代码,我已阅读有关消除错误但无法形成代码的帖子。
while True:
files=os.listdir(".")
#print(files)
# Get the MD5 hash for each -- use a for loop to process each file
for file_name in files:
with open(file_name , "rb") as fp:
file_content = fp.read()
md5 = hashlib.md5(file_content).hexdigest()
# should we compare the checksums too? XXXXXXXXX
# check for differences between new scan and old scan.
if not file_name in baseline.keys():
# print the name of any new files
print("new file detected!", file_name)
错误具体是:
Traceback(最近一次调用最后一次):文件“main.py”,第 31 行,在 使用 open(file_name , "rb") as fp: IsADirectoryError: [Errno 21] 是目录:'.upm'
如果有人可以帮助提供正确的代码,那将是非常有帮助的。
【问题讨论】:
-
错误是说您的当前目录 (
".") 有一个.upm是一个文件夹。您的问题是在循环listdir时如何忽略文件夹? -
没有 @GinoMempin ,我想使用 os.walk() 传递文件夹和文件。
-
但无法形成代码。你能帮我吗@GinoMempin。
-
您的代码中没有
os.walk。而且您不能在文件夹上使用open。当你到达一个文件夹时,你需要重复循环遍历该文件夹的每个文件。 -
stackoverflow.com/questions/52338706/… 。在这篇文章中,他们建议使用 os.walk() @GinoMempin
标签: python-3.x