ID : 396
viewed : 66
Tags : PythonPython File
99
This article introduces different methods to find files with a certain extension only in Python.
glob.glob
Method to Find Files With Certain ExtensionWe could use glob.glob
module to find the files with a certain extension only in Python.
import glob targetPattern = r"C:\Test\*.txt" glob.glob(targetPattern)
The above codes demonstrate how to find the files with extension txt
in the directory C:\Test
.
os.listdir()
Method to Find Files With Certain Extension function lists all the files in the given directory, without the file path information. You could extract the files with the specific extension by using str.endswith()
function.
>>> import os >>> fileDir = r"C:\Test" >>> fileExt = r".txt" >>> [_ for _ in os.listdir(fileDir) if _.endswith(fileExt)] ['test.txt', 'test1.txt']
You need to construct the full path with os.path.join()
function.
>>> import os >>> fileDir = r"C:\Test" >>> fileExt = r".txt" >>> [os.path.join(fileDir, _) for _ in os.listdir(fileDir) if _.endswith(fileExt)] ['C:\\Test\\test.txt', 'C:\\Test\\test1.txt']
pathlib.glob
Method to Find Files With Certain Extensionis introduced in Python 3.4 which offers object-oriented filesystem paths. It provides two styles: Windows paths in Windows OS and POSIX paths in Unix-alike systems.
>>> import pathlib >>> fileDir = r"C:\Test" >>> fileExt = r"*.txt" >>> list(pathlib.Path(fileDir).glob(fileExt)) [WindowsPath('C:/Test/test.txt'), WindowsPath('C:/Test/test1.txt')]
The result is represented with WindowsPath
, and you could convert the result to the string representation by adding str()
, like
>>> [str(_) for _ in pathlib.Path(fileDir).glob(fileExt)] ['C:\\Test\\test.txt', 'C:\\Test\\test.txt']
The pattern C:\Test\*.txt
only searches the txt
files in the directory C:\Test
, but not in its subdirectories. If you want to also get txt
files in the subdirectories, you could modify the pattern a bit.
import glob targetPattern = r"C:\Test\**\*.txt" glob.glob(targetPattern)
The wild cards **
between Test
and .txt
means it should find the txt
files both in the directory and its subdirectories.
pathlib
Module Search Files RecursivelySimilar to adding **
in glob.glob
to search files recursively, you can also add **
in pathlib.Path.glob
method to find the files with a certain extension recursively.
>>> import pathlib >>> fileDir = r"C:\Test" >>> fileExt = r"**\*.txt" >>> list(pathlib.Path(fileDir).glob(fileExt)) [WindowsPath('C:/Test/test.txt'), WindowsPath('C:/Test/test1.txt'), WindowsPath('C:/Test/sub/test1.txt')]