100%后,嵌套百分比停止在20%

时间:2017-10-03 09:26:28

标签: python python-3.x python-3.5

我正在迭代一个顶级目录下所有目录中的所有文件。我希望实现百分比的总体进展和个人进步。

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     

它有效,但更优雅的是什么?

0 个答案:

没有答案