我正在迭代一个顶级目录下所有目录中的所有文件。我希望实现百分比的总体进展和个人进步。
for root, dirs, files in os.walk(top_directory):
u = filter(lambda file: file.endswith('.pdf'), files)
laenge = len(list(u))
j = 1
for file in filter(lambda file: file.endswith('.pdf'), files):
print(str(round(j/laenge*100))+'% - Lese '+file)
j+=1
我用内部
成功了u1 = os.walk(top_directory)
laenge1 = len(list(u1))
s1 = 1
for root, dirs, files in os.walk(top_directory):
p1 = str(round(s1/laenge1*100))
s1+=1
u = filter(lambda file: file.endswith('.pdf'), files)
laenge = len(list(u))
j = 1
for file in filter(lambda file: file.endswith('.pdf'), files):
print(p1+'% - '+str(round(j/laenge*100))+'% - Lese '+file)
j+=1
然而p1
以20%结束。
我做了一件不光彩的
laenge1 = 0
for root, dirs, files in os.walk(top_directory):
for file in filter(lambda file: file.endswith('.pdf'), files):
laenge1+=1
s1 = 0
for root, dirs, files in os.walk(top_directory):
u = filter(lambda file: file.endswith('.pdf'), files)
laenge = len(list(u))
j = 0
for file in filter(lambda file: file.endswith('.pdf'), files):
p1 = str(round(s1/laenge1*100))
s1+=1
print(p1+'% - '+str(round(j/laenge*100))+'% - Lese '+file)
j+=1
它有效,但更优雅的是什么?