how to loop through files in a 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: python loop 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

Example 3: get all files within multiple directories python

from pathlib import Path
for f in Path().cwd().glob("../*.ext"):
    print(f)
    # do other stuff

Example 4: C# get all files in directory

//path is the path of the directory to get files from
//searchPattern is to get specific files. If you want only exe files you enter *.exe

private static IEnumerable<string> GetAllFiles(string path, string searchPattern)
        {
            return Directory.EnumerateFiles(path, searchPattern).Union(
            Directory.EnumerateDirectories(path).SelectMany(d =>
            {
                try
                {
                    return GetAllFiles(d, searchPattern);
                } catch(Exception e)
                {
                    return Enumerable.Empty<string>();
                }
            }));
        }