I want to iterate through the filenames in a particular folder. I then wish to choose the first filename that satisfies a criteria (file name ends with '.txt')
Should I use a For loop and break it when I see the first file ending with .txt? Or should I use a While loop? The While loop does not seem to work. It keeps on continuing. It keeps on printing the filename as per below code.
Following is the code I am using:
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