Find all text files in a directory with extension .txt in Python

edit  Updated by

Using glob

import glob, os

os.chdir("/my_folder")
for file in glob.glob("*.txt"):
    print(file)

Using os.listdir:

import os
for file in os.listdir("/my_folder"):
    if file.endswith(".txt"):
        print(os.path.join("/my_folder", file))

Using os.walk

import os
for root, dirs, files in os.walk("/my_folder"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))

See Also