1# using glob
2import glob, os
3os.chdir("/mydir")
4for file in glob.glob("*.txt"):
5 print(file)
6
7# using os.listdir
8import os
9for file in os.listdir("/mydir"):
10 if file.endswith(".txt"):
11 print(os.path.join("/mydir", file))
12
13# using os.walk
14import os
15for root, dirs, files in os.walk("/mydir"):
16 for file in files:
17 if file.endswith(".txt"):
18 print(os.path.join(root, file))