Python - How to list all the files
Sometimes, we need to list all the files in a folder or sub folder in Python.
In this post, I will show you how to use the glob
function to achieve this.
Solution
First, let’s take a look at the file structure of the example.
1 | ./data |
List all the files in single current folder
Now, we can do this by the following code.
1 | import glob |
Here is the output.
1 | ./data/file-5.txt |
List all the files in folder recursively
However, we also can list all the files recursively.
1 | import glob |
Now, we can see all the files in data
folder.
1 | ./data/file-5.txt |
Tips
Also, we can use regex
with glob
1 | import glob |
1 | ./data/file-3.txt |
🙂