How to create a list of files with a given extension (.txt, .avi, .docx, etc) in python ?

Published: May 21, 2019

DMCA.com Protection Status

Example of how to create a list of files with a given extension in python. Lets assume we want to create a list of all the files with the extensions '.hdf' in a folder called 'output':

./output/
    MYD06_L2.A2014038.2300.061.2018052102433.hdf
    MYD06_L2.A2014033.1930.061.2018052042944.hdf
    MYD06_L2.A2014046.2210.061.2018052112538.hdf
    MYD06_L2.A2014058.2235.061.2018052141003.hdf
    MYD06_L2.A2014037.2045.061.2018052094723.hdf
    MYD06_L2.A2014054.2125.061.2018052131042.hdf
    MYD06_L2.A2014034.2015.061.2018052062330.hdf
    MYD06_L2.A2014059.2010.061.2018052140044.hdf
    .
    .
    .

A solution is to use the python module glob:

import glob

path_to_target = './outputs/modis/myd06/'

path_to_file_list = glob.glob(path_to_target + '*hdf' )

for path_to_file in path_to_file_list:
    print(path_to_file)

which returns

./outputs/modis/myd06/MYD06_L2.A2014038.2300.061.2018052102433.hdf
./outputs/modis/myd06/MYD06_L2.A2014033.1930.061.2018052042944.hdf
./outputs/modis/myd06/MYD06_L2.A2014046.2210.061.2018052112538.hdf
./outputs/modis/myd06/MYD06_L2.A2014058.2235.061.2018052141003.hdf
./outputs/modis/myd06/MYD06_L2.A2014037.2045.061.2018052094723.hdf
./outputs/modis/myd06/MYD06_L2.A2014054.2125.061.2018052131042.hdf
./outputs/modis/myd06/MYD06_L2.A2014034.2015.061.2018052062330.hdf
./outputs/modis/myd06/MYD06_L2.A2014059.2010.061.2018052140044.hdf
.
.
.

Now to create a list with the filename only, we can use the python list comprehensions de python:

file_list = [i.split('/')[-1] for i in path_to_file_list]

print(file_list)

gives:

['MYD06_L2.A2014038.2300.061.2018052102433.hdf', 'MYD06_L2.A2014033.1930.061.2018052042944.hdf', 'MYD06_L2.A2014046.2210.061.2018052112538.hdf', 'MYD06_L2.A2014058.2235.061.2018052141003.hdf', 'MYD06_L2.A2014037.2045.061.2018052094723.hdf', ... ]

References

Links Site
glob docs.python.org
List Comprehensions in Python pythonforbeginners.com
os.listdir() python doc
How to list all files of a directory in Python stackoverflow
Calling an external command in Python stackoverflow