You could have at least 3 methods to list all the files of a directory in Python, by using Python functions
Below is the presumption of this tutorial,
- Python version - Python 3
- The path name is
dirPath and it exists in the system, therefore, we will not need to check the existence of the directory.
os.listdir to Get All Files in a Directory in Python
os.listdir lists all the files and folders in the directory, therefore it needs extra code to filter files from the result.
import os dirPath = r"C:\git\DelftStack\content" result = [f for f in os.listdir(dirPath) if os.path.isfile(os.path.join(dirPath, f))] print(result)
os.listdir returns only the relative path of the files or folders with reference to
os.path.isfile function needs the full path to check whether it is a file or not, hence we need to use
os.path.join to combine the
dirPath and the results of
os.listdir to get the full path of either files or paths.
os.walk to Get All Files in a Directory in Python
os.walk generates the file names in the given directory by traversing the tree top-down (per default) or bottom-up. It yields a 3-tuple (
filenames) each time it walks to the directory in the tree (including top itself).
All the files are included in the tuple for the first yield of
os.walk, therefore a Pythonic way is
import os dirPath = r"C:\git\DelftStack\content" result = next(os.walk(dirPath)) print(result)
glob.glob to Get All Files in a Directory in Python
glob module finds all the pathnames matching the given specific pattern according to the rules used in the Unix shell.
glob.glob returns the list of path names that match the given path name pattern. The file path has the pattern of
*.*, that is what will be passed to
glob.glob as the input argument.
import glob dirPathPattern = r"C:\git\DelftStack\content\*.*" result = glog.glob(dirPathPattern) print(result)
glob.glob returns the full path of the matched files, like
The result of
glob.glob method as shown here couldn’t guarantee they are files-only because it only checks whether the path name matches the pattern, but not checks it is a file or a directory.
For example, if a directory has the name pattern like
test.test, then this directory is also included in the result.
If you need to ensure that the output contains only files, you need to verify it with the