Get all files with a certain extension in Python

The simplest way to get all files with py extension is using glob module in Python.

import glob

a = glob.glob('*.py')
b = glob.glob('./*.py')

print(a)
# ['tuple.py', 'class2.py', 'animal.py', 'metaclass.py', 'dic.py', 'integer.py', 'country.py', 'math2.py', 'class.py', 'helper.py', 'math.py', 'array.py', 'timezone.py', 'chicken.py', 'complex.py']

print(b)
# ['./tuple.py', './class2.py', './animal.py', './metaclass.py', './dic.py', './integer.py', './country.py', './math2.py', './class.py', './helper.py', './math.py', './array.py', './timezone.py', './chicken.py', './complex.py']

The argument of glob.glob usually has an asterisk meaning an arbitrary string. The above example shows how to get all .py files in the directory that has the current Python file. The code is written in helper.py and this is contained in the return list.

Python File

Python Tutorial