Python find all files in path

Python List Files in a Directory

In this article, we will see how to list all files of a directory in Python. There are multiple ways to list files of a directory. In this article, We will use the following four methods.

  • os.listdir(‘dir_path’) : Return the list of files and directories present in a specified directory path.
  • os.walk(‘dir_path’) : Recursively get the list all files in directory and subdirectories.
  • os.scandir(‘path’) : Returns directory entries along with file attribute information.
  • glob.glob(‘pattern’) : glob module to list files and folders whose names follow a specific pattern.

Table of contents

How to List All Files of a Directory

Getting a list of files of a directory is easy as pie! Use the listdir() and isfile() functions of an os module to list all files of a directory. Here are the steps.

  1. Import os module This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.
  2. Use os.listdir() function The os.listdir(‘path’) function returns a list containing the names of the files and directories present in the directory given by the path .
  3. Iterate the result Use for loop to Iterate the files returned by the listdir() function. Using for loop we will iterate each file returned by the listdir() function
  4. Use isfile() function In each loop iteration, use the os.path.isfile(‘path’) function to check whether the current path is a file or directory. If it is a file, then add it to a list. This function returns True if a given path is a file. Otherwise, it returns False.
Читайте также:  Input text boxes html

Example to List Files of a Directory

Let’s see how to list files of an ‘account’ folder. The listdir() will list files only in the current directory and ignore the subdirectories.

Example 1: List only files from a directory

import os # folder path dir_path = r'E:\\account\\' # list to store files res = [] # Iterate directory for path in os.listdir(dir_path): # check if current path is a file if os.path.isfile(os.path.join(dir_path, path)): res.append(path) print(res)

Here we got three file names.

['profit.txt', 'sales.txt', 'sample.txt']

If you know generator expression, you can make code smaller and simplers using a generator function as shown below.

Generator Expression:

import os def get_files(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)): yield file

Then simply call it whenever required.

for file in get_files(r'E:\\account\\'): print(file)

Example 2: List both files and directories.

Directly call the listdir(‘path’) function to get the content of a directory.

import os # folder path dir_path = r'E:\\account\\' # list file and directories res = os.listdir(dir_path) print(res)

As you can see in the output, ‘reports_2021’ is a directory.

['profit.txt', 'reports_2021', 'sales.txt', 'sample.txt']

os.walk() to list all files in directory and subdirectories

The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).

Note: Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.

It is a recursive function, i.e., every time the generator is called, it will follow each directory recursively to get a list of files and directories until no further sub-directories are available from the initial directory.

For example, calling the os.walk(‘path’) will yield two lists for each directory it visits. The first list contains files, and the second list includes directories.

Let’s see the example to list all files in directory and subdirectories.

from os import walk # folder path dir_path = r'E:\\account\\' # list to store files name res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) print(res)
['profit.txt', 'sales.txt', 'sample.txt', 'december_2021.txt']

Note: Add break inside a loop to stop looking for files recursively inside subdirectories.

from os import walk # folder path dir_path = r'E:\\account\\' res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) # don't look inside any subdirectory break print(res) 

os.scandir() to get files of a directory

The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

It returns an iterator of os.DirEntry objects, which contains file names.

import os # get all files inside a specific folder dir_path = r'E:\\account\\' for path in os.scandir(dir_path): if path.is_file(): print(path.name)
profit.txt sales.txt sample.txt

Glob Module to list Files of a Directory

The Python glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern.

For example, to get all files of a directory, we will use the dire_path/*.* pattern. Here, *.* means file with any extension.

Let’s see how to list files from a directory using a glob module.

import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\account\*.*' res = glob.glob(dir_path) print(res)
['E:\\account\\profit.txt', 'E:\\account\\sales.txt', 'E:\\account\\sample.txt']

Note: If you want to list files from subdirectories, then set the recursive attribute to True.

import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\demos\files_demos\account\**\*.*' for file in glob.glob(dir_path, recursive=True): print(file)
E:\account\profit.txt E:\account\sales.txt E:\account\sample.txt E:\account\reports_2021\december_2021.txt

Pathlib Module to list files of a directory

From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.

  • Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
  • Next, Use the pathlib.Path(‘path’) to construct directory path
  • Next, Use the iterdir() to iterate all entries of a directory
  • In the end, check if a current entry is a file using the path.isfile() function
import pathlib # folder path dir_path = r'E:\\account\\' # to store file names res = [] # construct path object d = pathlib.Path(dir_path) # iterate directory for entry in d.iterdir(): # check if it a file if entry.is_file(): res.append(entry) print(res)

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ

Источник

How to Find all files in a Directory with specific extension in Python

How to Find all files in a Directory with specific extension in Python

Python comes with a standard module called os that is used to handle file management using Python. With the help of Python os modules, we can perform many file management tasks like creating, renaming, moving, copying, searching, and deleting files and directories. If you want to know more about Python file management with the os module then click here .

In this tutorial, we will not be covering all the important methods of the os module. Instead, we will be using it to find specific extension files from a directory. For example, we will be writing a python script that can find all the .txt, .doc, .py, .jgeg, etc., files from a specific directory.

Python program to find all the .txt files from a directory

We will start with finding all the .txt files present in a specific directory. For this tutorial, I will be searching all the .txt files present in the same directory where my Python script is located and printing the complete path as an output.

import os for file in os.listdir(): if file.endswith(".txt"): print(os.path.join(os.getcwd(),file))
C:\Users\tsmehra\Desktop\code\data.txt C:\Users\tsmehra\Desktop\code\external_css.txt C:\Users\tsmehra\Desktop\code\external_script.txt C:\Users\tsmehra\Desktop\code\passwords_list.txt C:\Users\tsmehra\Desktop\code\vulnarable_banners.txt

The os.listdir() function will return a list of all the files and directories present in the current directory. The .endswith() is a Python string function that will check if a file ends with an extension of .txt . The os.getcwd() function returns the absolute path of the current working directory. The os.path.join() method will join the current working directory path with the file name. In the above example, I have listed all the .txt files that are present in the same directory where the Python script is located. If you want to find files of different directories there you need to change the working directory by using the os.chdir() method.

import os directory = r'C:\Users\tsmehra\Documents' os.chdir(directory) #change the current working directory for file in os.listdir(): if file.endswith(".txt"): print(os.path.join(os.getcwd(),file))
C:\Users\tsmehra\Documents\allnew.txt C:\Users\tsmehra\Documents\config.txt C:\Users\tsmehra\Documents\Python has many built.txt

It’s very important to use the r»» prefix before the directory name else we need to specify the escape characters.

Python program to find all the Python .py files from a directory

The program will remain the same. The only change we need to make in order to retrieve all the .py files is in the endswith() method.

import os #directory = r'' #os.chdir(directory) #change the current working directory for file in os.listdir(): if file.endswith(".py"): #only python .py files print(os.path.join(os.getcwd(),file))
C:\Users\tsmehra\Desktop\code\assambaly.py C:\Users\tsmehra\Desktop\code\attack.py C:\Users\tsmehra\Desktop\code\checkweather.py C:\Users\tsmehra\Desktop\code\client.py C:\Users\tsmehra\Desktop\code\colorful.py C:\Users\tsmehra\Desktop\code\compareimage.py C:\Users\tsmehra\Desktop\code\contours.py C:\Users\tsmehra\Desktop\code\crackpassword.py C:\Users\tsmehra\Desktop\code\CssJSlinks.py C:\Users\tsmehra\Desktop\code\dDosattqack.py C:\Users\tsmehra\Desktop\code\deconde.py C:\Users\tsmehra\Desktop\code\DecryptFile.py . 

Python program to find all the Images .jpeg, .jpg, .png files from a directory

Now let’s find all the images present in a specific directory. The code will remain pretty the same as we have written for the above examples, but here will be making some changes in the conditional if statement.

import os directory = r'C:\Users\tsmehra\Pictures' os.chdir(directory) #change the current working directory to pictures for file in os.listdir(): if file.split(".")[-1].lower() in ["apng", "avif", "gif","jpeg", "jpg", "png", "svg"]: print(os.path.join(os.getcwd(),file))
C:\Users\tsmehra\Pictures\Armstrong_Number_Python.jpg C:\Users\tsmehra\Pictures\Arrays-data-structure.png C:\Users\tsmehra\Pictures\arrays.png C:\Users\tsmehra\Pictures\atom.png C:\Users\tsmehra\Pictures\best python libraries 2021.jpg C:\Users\tsmehra\Pictures\blur faces with open cv.jpg C:\Users\tsmehra\Pictures\choosepython.jpg C:\Users\tsmehra\Pictures\contours image opencv python.jpg C:\Users\tsmehra\Pictures\contours on the blank image.jpg C:\Users\tsmehra\Pictures\coolpad python online copiler.jpg

Conclusion

Let’s sum up the above Python tutorial. In this tutorial, you learned how to find specific file extensions in Python. The module we used in our tutorial is os which is a Python standard module for file and directory management. If you are searching for the files that are present in the same directory of your Python script, then you do not need to change the working directory, but if you wish to find files from another directory there, you need to change the working directory using the os.chdir() method. The os.listdir() will list out all the directories and files present in the current working directory, and using the if statement and endswith() statement we can find the specific extension files.

People are also reading:

Источник

Оцените статью