Answers for "python iterate through folder directory"

20

how to iterate through files in a folder 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
Posted by: Guest on March-30-2020
0

iterate through directories in python

import os

dir = os.fsencode(filename='img') # here 'img' should be replaced with
									# desired directory

lst = []

for items in os.listdir(dir):
    lst.append(items)
print(lst)
Posted by: Guest on December-26-2021

Code answers related to "python iterate through folder directory"

Python Answers by Framework

Browse Popular Code Answers by Language