how to iterate through every item in directory python code example
Example 1: iterate through all files in directory python
import os
directory = 'the/directory/you/want/to/use'
for filename in os.listdir(directory):
if filename.endswith(".txt"):
#do smth
continue
else:
continue
Example 2: python iterate directory
import os
directory = r'C:\Users\admin'
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
print(os.path.join(directory, filename))
else:
continue