python list files in directory with extension code example
Example 1: python list files in current directory
import os
files = os.listdir('.')
print(files)
for file in files:
# do something
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: get list of files in directory python
import os
path = '/folder1/folder2/'
files = os.listdir(path)
Example 4: list files in directory python with extension
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)