This tutorial demonstrates getting all files from a directory with a Python code example. For someone looking for how to list all files from a directory in Python this is correct place to see with short example. You will understand how to show all files in a directory with Python programming. The given example below of how to get list all files from folder directory in Python. So let's see below sample example in Python getting all files in sub-directory with sample output.
The below code to list all files in current directory without recursively in sub-directory.
Create file main.py
import os, glob
files = glob.glob('folder/*.*')
print(files);
['folder/my_noted.txt', 'folder/meme.jpeg', 'folder/tutorial.docx']
Suppose we have directory named "folder" and in that directory have files and other directories as well, in this case we will recursivly walkthrought all subdirectories to find all files.
Create file main.py
from os import walk
# root folder path
path = 'folder'
# list to store files name
files = []
for (path, dirName, fileName) in walk(path):
files.extend(fileName)
print(files )
['my_noted.txt', 'meme.jpeg', 'tutorial.docx', 'demo.png']
Hope you can understand and know how to get list file in directories. Have a nice day!!
You may also like...
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him