Py学习  »  Python

Python:遍历文件夹并选择以.txt结尾的第一个文件

rmore911 • 5 年前 • 1820 次点击  

当我看到第一个以.txt结尾的文件时,应该使用For循环并中断它吗? While循环似乎不起作用。它一直在继续。它继续按照下面的代码打印文件名。

下面是我正在使用的代码:

import os
import pdb

asciipath = 'C:\\Users\\rmore\\Desktop\\Datalab'

x = 0

file = os.listdir(asciipath)

while file[x].endswith('.txt'):
    print(file[x])
    x = x+1
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56597
 
1820 次点击  
文章 [ 3 ]  |  最新文章 5 年前