for循环中的重复迭代

时间:2019-02-12 12:06:19

标签: python for-loop

我的目录中有3个docx。 abcd.docxuvwx.docxqwer.docx。我正在遍历这些文件。在这里,我得到了另一个迭代,其中最后一个迭代打印了er.docx。也就是说,它会跳过前两个字符,并显示最后一个文件的其余部分。下面是代码和输出。

for filename in os.listdir(directory):
    if filename.endswith(".docx"): 
        print(os.path.join(directory, filename))

输出:

filepath\abcd.docx     
filepath\uvwx.docx
filepath\qwer.docx
filepath\er.docx                       

为什么最后一次迭代会发生?

0 个答案:

没有答案