python for all files in directory code example

Example 1: for every file in the folder do python

for file in os.listdir(inputdir):

Example 2: 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 3: python list all files in directory

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Example 4: python list all files in directory

import os
 arr = os.listdir()
 print(arr)
 
 >>> ['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']

Example 5: read all files in folder python

import glob
print(glob.glob("/home/adam/*.txt"))