for file in folder 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: for every file in the folder do python

for file in os.listdir(inputdir):

Example 3: how to search for a specific file extension with python

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)

Example 4: how to loop through files in a directory python

import os

for filename in os.listdir(directory):
    if filename.endswith(".asm") or filename.endswith(".py"): 
         # print(os.path.join(directory, filename))
        continue
    else:
        continue

Example 5: python iterate through files in directory

import os

for filename in os.listdir(directory):
    if filename.endswith(".asm") or filename.endswith(".py"): 
         # print(os.path.join(directory, filename))
        continue
    else:
        continue