Python Get All Files from Directory Example

Hi Guys,
This is a short guide on python get all files from directory. I explained simply step by step python get all files in directory example. In this article, we will implement a how to get all files from folder in python. This tutorial will give you simple example of how to get all files from directory in python.
In this example, we'll use Python to retrieve a list of all the files in a directory. We'll use "glob" and "walk" to retrieve a list of files recursively from the "os" library. Let's look at few examples below.
So let's see bellow example:
Example 1: List of All Files from Directory
Simply get all files from folder without recursively. we will get all files from "files" folder.
main.pyimport os, glob fileList = glob.glob('files/*.*') print(fileList);Output
['files/test.txt', 'files/demo.png']
Example 2: List of All Files from Directory Recursively
Just recursively get every file in the folder. All files will be obtained from the "files" directory and its subdirectories.
main.pyfrom os import walk # folder path dirPath = 'files' # list to store files name res = [] for (dirPath, dirName, fileName) in walk(dirPath): res.extend(fileName) print(res)Output
['test.txt', 'demo.png', 'demo.txt']
It will help you....
Happy Pythonic Coding!