Examples of how to get a list of files located in a folder using python:
Table of contents
Using listdir() function
Lets assume a folder that contains:
dir01/
dir02/
fich01.txt
fich02.txt
fich02.txt
To get a list of files located in the current folder using python a first solution is to use os.listdir():
>>> import os
>>> list = os.listdir('.')
>>> print(list)
[dir01,dir02,fich01.txt,fich02.txt,fich03.txt]
However listdir() returns the files and the folders as well. To get the files only a solution is to use isfile() (see):
>>> FichList = [ f for f in os.listdir('.') if os.path.isfile(os.path.join('.',f)) ]
>>> print( FichList )
[fich01.txt,fich02.txt,fich03.txt]
Note: it is possible to define directly the path to the folder:
>>> list = os.listdir('path_to_folder')
Using glob() function
If the goal is to get all the files with a given extensions, for example .hdf:
./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
.
.
.
another solution is to use 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)
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
.
.
.
To get only the file names we can use a list comprehension:
file_list = [i.split('/')[-1] for i in path_to_file_list]
print(file_list)
returns
['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', ... ]
Using walk() function
A third solution is to use the module os with walk. Lets consider the following case:
folder/
abcd.txt
data.txt
record.txt
sub_directory/
data.txt
folder_3/
To get a list of all the files:
>>> import os
>>> for path, subdirs, files in os.walk('folder'):
... for name in files:
... print(os.path.join(path, name))
...
folder/abcd.txt
folder/data.txt
folder/record.txt
folder/sub_directory/data.txt
References
Links | Site |
---|---|
os.listdir() | python doc |
How to list all files of a directory in Python | stackoverflow |
Calling an external command in Python | stackoverflow |
glob | docs.python.org |
List Comprehensions in Python | pythonforbeginners.com |
os.walk | python doc |
Python list directory, subdirectory, and files | stackoverflow |