The os.listdir method helps us to list directory contents. The fnmatch.fnmatch method helps us find files that are named a certain way (e.g. have a certain extension).
Here is a function that returns a list of .txt files in a directory:
import fnmatch import os def get_files_by_ext(dirpath, ext): files =  for file in os.listdir(dirpath): if fnmatch.fnmatch(file, '*.' + ext): files.append(os.path.join(dirpath, file).replace("\", "/")) return files text_files = get_files_by_ext('c:/myfiles', 'txt') print(','.join(text_files))
The output is:
with c:/myfiles as follows:
Note that the fnmatch function is not case sensitive (it matches .txt and .TXT)