My objective is to print out all available files that end in '.txt' inside a folder, I'm unsure how to do so. Thank you for reading.
Asked
Active
Viewed 874 times
-6
-
You could have easily googled this one. – Vineeth Sai Nov 05 '18 at 05:17
-
Hint: Use `glob` module – Mohit Solanki Nov 05 '18 at 05:18
-
`print([f for f in os.listdir('/path/to/folder') if f.endswith('.txt')])` – ksbg Nov 05 '18 at 05:18
-
1@ksbg Lists shouldn't be used to print – Vineeth Sai Nov 05 '18 at 05:19
-
1`import os for file in os.listdir(path): if file.endswith('.txt'): print(file)` – RoadRunner Nov 05 '18 at 05:19
-
Note if your running any sort of UNIX environment you could also just run `find
-type f -name "*.txt"`. – RoadRunner Nov 05 '18 at 05:22
1 Answers
0
you can use
filename.endswith('.txt')
simply just open the folder by
fileList = os.listdir("folder_path") #open that folder
for filename in fileList:
if(filename.endswith('.txt')): # check its extension
print(filename)

Rishabh Jhalani
- 1,049
- 13
- 22