Home Code List all files in a directory in Python

List all files in a directory in Python

We can get a list of files in a directory or folder in Python using the function os.listdir which takes the folder path as an argument and lists the names of all files and folders inside the given directory.

List names of all files and folders in a directory

If we simply want to find the names of all files and folders in a directory, then use the following Python code snippet by replacing folder_path with your folder path.

from os import listdir
files_folders = [file_folder_name for file_folder_name in listdir(folder_path) ]Code language: Python (python)

List names of files only in a directory in Python

If you want to list only files in a directory, then use the following Python code snippet by replacing folder_path with your folder path. This works only in Python 3.5 or later..

from os import scandir
folders_only = [folder.name for folder in scandir(folder_path) if folder.is_file() ]Code language: Python (python)

Alternately, for versions of Python older than 3.5 you can use the following code to list file names.

from os import listdir
from os.path import isfile, join
files_only = [file_name for file_name in listdir(folder_path) if isfile(join(folder_path,file_name))]Code language: Python (python)

List names of sub folders in a directory in Python

If you want to list only sub directories in a directory, then use the following Python code snippet by replacing folder_path with your folder path. This works only in Python 3.5 or later.

from os import scandir
folders_only = [folder.name for folder in scandir(folder_path) if folder.is_dir() ]Code language: Python (python)

Alternately, for versions of Python older than 3.5 you can use the following code to list names of sub directories.

from os import listdir
from os.path import isfile, join
folders_only = [folder_name for folder_name in listdir(folder_path) if not isfile(join(folder_path,folder_name))]Code language: Python (python)

List all files with a given extension only in Python

If you want to list only those files in a directory having a given extension, you can do it in Python as follows. Replace ‘extension’ with you extension like ‘.txt’ or ‘.jpg’ or ‘.pdf’.

from os import listdir
from os.path import isfile, join
files_only = [file_name for file_name in listdir(folder_path) if isfile(join(folder_path,file_name)) and file_name.endswith(extension)]Code language: Python (python)

List files in a directory using glob

Alternately, you can use glob to list all files in a directory. The benefit of using glob is that it allows patterns to be used to match file names. It also returns the complete path of the each file. For instance to find all text files in a folder using glob the corresponding code would be:

import glob
text_files_with_path = glob.glob("/my_folder_path/*.txt")Code language: Python (python)
Content Protection by DMCA.com

Download HitXP Mobile App

Get it on Google Play
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

The Power of Democracy: How Free Societies Foster Innovation in Science and Technology

Democracy allows freedom of speech and thought, boosting creativity and innovation. This leads to scientific breakthroughs and technological advancements.

How does GOD helps us?

Do prayers really help? Do miracles happen when we are in trouble? Does God really help us in times of need? Are prayers enough to save us during bad times? How should we face problems?

Sanskrit poem on Ramayana – read in reverse becomes Mahabharata!

A Sanskrit poem about Ramayana when read in reverse direction becomes a poem on Mahabharata!

The difference between Itihasa and Puranas

Documentation of ancient Indian history and the historical events of Indian civilization in the form of Itihasa and Puranas - Ramayana and Mahabharata.

Latest Music Notations

Jai Shri Ram – Adipurush – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Jai Shri Ram from the 2023 Hindi movie Adipurush in Western and Indian Notations.

Sojugada Soojumallige – Garuda Gamana Vrishabha Vahana – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Sojugada Soojumallige from the 2021 Kannada movie Garuda Gamana Vrishabha Vahana in Western and Indian Notations.

Oo Antava Mava – Pushpa – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Oo Antava Mava from the 2022 Telugu movie Pushpa in Western and Indian Notations.

Kaa Chalige Bangalore (Tiningaa Miningaa Tishaaaa) – Salaga – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Tiningaa Miningaa Tishaaaa (Kaa Chali Ge) from the 2022 Kannada movie Salaga in Western and Indian Notations.
Content Protection by DMCA.com