Delete (Remove) Files and Directories in Python (2024)

In this tutorial, you’ll learn how to delete files or directories in Python.

After reading this tutorial, you’ll learn: –

  • Deleting file using the os module and pathlib module
  • Deleting files from a directory
  • Remove files that match a pattern (wildcard)
  • Delete empty directory
  • Delete content of a directory (all files and sub directories)

Sometimes we need to delete files from a directory that is no longer needed. For example, you are storing monthly inventory data in a file. You may want to delete any existing data files before creating a new data file each month.

Also, after some time, the application needs to delete its old log files.

In this tutorial, we will use the following Python functions to delete files and folders.

FunctionDescription
os.remove('file_path')Removes the specified file.
os.unlink('file_path')Removes the specified file. Useful in UNIX environment.
pathlib.Path("file_path").unlink()Delete the file or symbolic link in the mentioned path
os.rmdir('empty_dir_path')Removes the empty folder.
pathlib.Path(empty_dir_path).rmdir()Unlink and delete the empty folder.
shutil.rmtree('dir_path')Delete a directory and the files contained in it.

Note:

  • All above functions delete files and folders permanently.
  • The pathlib module was added in Python 3.4. It is appropriate when your application runs on a different operating systems.

Table of contents

  • How to Delete a File in Python
    • Example: Remove File in Python
    • Understand the os.remove() method
    • Check if File Exist Before Deleting It
  • Remove File Using os.unlink() method
  • Pathlib Module to Remove File
  • Delete all Files from a Directory
    • Delete an Empty Directory (Folder) using rmdir()
    • Delete a Non-Empty Directory using shutil
  • Deleting Files Matching a Pattern
    • Example: Deleting Files with Specific Extension
    • Delete file whose name starts with specific string
    • Delete file whose name contains a specific letters
    • Deleting Files Matching a Pattern from all Subfolders
  • Conclusion

How to Delete a File in Python

Python provides strong support for file handling. We can delete files using different methods and the most commonly used one is the os.remove() method. Below are the steps to delete a file.

  1. Find the path of a file

    We can delete a file using both relative path and absolute path. The path is the location of the file on the disk.
    An absolute path contains the complete directory list required to locate the file. And A relative path includes the current directory and then the file name.
    For example,/home/Pynative/reports/samples.txtis an absolute path to discover the samples.txt.

  2. Use os.remove() function to delete File

    The OS module in Python provides methods to interact with the Operating System in Python. The remove() method in this module is used to remove/delete a file path.
    First, import the os module and Pass a file path to the os.remove('file_path') function to delete a file from a disk

  3. Use the rmtree() function of shutil module to delete a directory

    Import the shutil module and pass the directory path to shutil.rmtree('path') function to delete a directory and all files contained in it.

Example: Remove File in Python

The following code explains how to delete a file named “sales_1.txt”.

Let’s assume we want to delete the sales_1.txt file from the E:\demos\files\ directory. Right now, this directory contains the following files:

  1. sales_1.txt
  2. sales_2.csv
  3. profits.txt
  4. revenue.txt

Remove file with relative path

import os# removing a file with relative pathos.remove("sales_1.txt")Code language: Python (python)

Remove file with absolute path

import os# remove file with absolute pathos.remove(r"E:\demos\files\sales_2.txt")Code language: Python (python)

Our code deleted two files. Here is a list of the remaining files in our directory:

  • profits.txt
  • revenue.txt

Understand the os.remove() method

Syntax:

os.remove(path, *, dir_fd = None)Code language: Python (python)

Pass file path to the os.remove('file_path') function to delete a file from a disk

The following are the parameters that we need to pass.

  • path – A relative or absolute path for the file object generally in string format.
  • dir_fd – A directory representing the location of the file. The default value is none and this value is ignored in the case of an absolute path.

If the passed file path is a directory, an OSError will be raised

Check if File Exist Before Deleting It

A FileNotFoundError will be raised if the file is not found in the path so it is advisable to check if the file exists before deleting it.

This can be achieved in two ways:

  • os.path.exists("file path") function to check if file exists.
  • Use exception handling.

Example 1:

import osfile_path = r'E:\demos\files\sales_2.txt'if os.path.exists(file_path): os.remove(file_path)else: print("The system cannot find the file specified")Code language: Python (python)

Note: Exception handling is recommended over file check because the file could be removed or changed in between. It is the Pythonic way to delete a file that may or may not exist.

Example 2: Exception handling

import osfile_path = r'E:\demos\files\sales_21.txt'try: os.remove(file_path)except: print("The system cannot find the file specified") # your codeCode language: Python (python)

Remove File Using os.unlink() method

If you are using the UNIX operating system use the unlink() method available in the OS module, which is similar to the remove() except that it is more familiar in the UNIX environment.

os.unlink(path, *, dir_fd=None)Code language: Python (python)
  • path – A relative or absolute path for the file object generally in string format.
  • dir_fd – A directory representing the location of the file. The default value is none and this value is ignored in the case of an absolute path.

Let us see the code for deleting the file “profits.txt” which is in the current execution path.

import osos.unlink('profits.txt')Code language: Python (python)

Pathlib Module to Remove File

The pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems. Thus, whenever we need to work with files in multiple environments, we can use the pathlib module.

The pathlib module was added in Python 3.4. The pathlib.path.unlink() method in the pathlib module is used to remove the file in the mentioned path.

Also, it takes one extra parameter, namely missing_ok=False. If the parameter is set to True, then the pathlib module ignores the File Not Found Error. Otherwise, if the path doesn’t exist, then the FileNotFoundError will be raised.

Let us see the code for deleting the file “profits.txt” which is present in the current execution path.

  • Import a pathlib module
  • Use pathlib.Path() method to set a file path
  • Next, to delete a file call the unlink() method on a given file path.
import pathlib# Setting the path for the filefile = pathlib.Path("profits.txt")# Calling the unlink method on the pathfile.unlink()Code language: Python (python)

Delete all Files from a Directory

Sometimes we want to delete all files from a directory without deleting a directory. Follow the below steps to delete all files from a directory.

  • Get the list of files in a folder using os.listdir(path) function. It returns a list containing the names of the files and folders in the given directory.
  • Iterate over the list using a for loop to access each file one by one
  • Delete each file using the os.remove()

Example:

import ospath = r"E:\demos\files\reports\\"for file_name in os.listdir(path): # construct full file path file = path + file_name if os.path.isfile(file): print('Deleting file:', file) os.remove(file)Code language: Python (python)

Delete an Empty Directory (Folder) using rmdir()

While it is always the case that a directory has some files, sometimes there are empty folders or directories that no longer needed. We can delete them using the rmdir() method available in both the os module and the pathlib module.

Using os.rmdir() method

In order to delete empty folders, we can use the rmdir() function from the os module.

os.rmdir(path, *, dir_fd = None)Code language: Python (python)

The following are the parameters that we need to pass to this method.

  • path – A relative or absolute path for the directory object generally in string format.
  • dir_fd – File directory. The default value is none, and this value is ignored in the case of an absolute path.

Note: In case if the directory is not empty then the OSError will be thrown.

import os# Deleting an empty folderdirectory = r"E:\pynative\old_logs"os.rmdir(directory)print("Deleted '%s' directory successfully" % directory)Code language: Python (python)

Output

Deleted 'E:\pynative\old_logs' directory successfully 

Use pathlib.Path.rmdir()

The rmdir() method in the pathlib module is also used to remove or delete an empty directory.

  • First set the path for the directory
  • Next, call the rmdir() method on that path

Let us see an example for deleting an empty directory called ‘Images’.

import pathlib# Deleting an empty folderempty_dir = r"E:\pynative\old_images"path = pathlib.Path(empty_dir)path.rmdir()print("Deleted '%s' directory successfully" % empty_dir)Code language: Python (python)

Delete a Non-Empty Directory using shutil

Sometimes we need to delete a folder and all files contained in it. Use the rmtree() method of a shutil module to delete a directory and all files from it. See delete a non-empty folder in Python.

The Python shutil module helps perform high-level operations in a file or collection of files like copying or removing content.

shutil.rmtree(path, ignore_errors=False, onerror=None)Code language: Python (python)

Parameters:

  • path – The directory to delete. The symbolic links to a directory are not acceptable.
  • ignore_errors – If this flag is set to true, then the errors due to failed removals will be ignored. If set to true, the error should be handler by the function passed in the one error attribute.

Note: The rmtree() function deletes the specified folder and all its subfolders recursively.

Consider the following example for deleting the folder ‘reports’ that contains image files and pdf files.

import shutil# Deleting an non-empty folderdir_path = r"E:\demos\files\reports"shutil.rmtree(dir_path, ignore_errors=True)print("Deleted '%s' directory successfully" % dir_path)Code language: Python (python)

Output

Deleted 'E:\demos\files\reports' directory successfully 

Get the proper exception message while deleting a non-empty directory

In order to get the proper exception message we can either handle it in a separate function whose name we can pass in the oneerror parameter or by catching it in the try-except block.

import shutil# Function for Exception Handlingdef handler(func, path, exc_info): print("We got the following exception") print(exc_info)# locationdir_path = r'E:\demos\files\reports'# removing directoryshutil.rmtree(dir_path, ignore_errors=False, onerror=handler)Code language: Python (python)

Final code: To delete File or directory

import osimport shutildef delete(path): """path could either be relative or absolute. """ # check if file or directory exists if os.path.isfile(path) or os.path.islink(path): # remove file os.remove(path) elif os.path.isdir(path): # remove directory and all its content shutil.rmtree(path) else: raise ValueError("Path {} is not a file or dir.".format(path))# filedelete(r'E:\demos\files\reports\profits.txt')# directorydelete(r'E:\demos\files\reports')Code language: Python (python)

Deleting Files Matching a Pattern

For example, you want to delete files if a name contains a specific string.

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

glob.glob(pathname, *, recursive=False)Code language: Python (python)

The glob.glob() method returns a list of files or folders that matches the pattern specified in the pathname argument.

This function takes two arguments, namely pathname, and recursive flag ( If set toTrueit will search files recursively in all subfolders)

We can use the wildcard characters for the pattern matching, and the following is the list of the wildcard characters used in the pattern matching.

  • Asterisk (*): Matches zero or more characters
  • Question Mark (?) matches exactly one character
  • We can specify a range of alphanumeric characters inside the[].

Example: Deleting Files with Specific Extension

On certain occasions, we have to delete all the files with a particular extension.

  • Use glob() method to find all text files in a folder
  • Use for loop to iterate all files
  • In each iteration, delete single file.

Let us see few examples to understand how to use this to delete files that match a specific pattern.

Example

import globimport os# Search files with .txt extension in current directorypattern = "*.txt"files = glob.glob(pattern)# deleting the files with txt extensionfor file in files: os.remove(file)Code language: Python (python)

Delete file whose name starts with specific string

import globimport os# Delete file whose name starts with string 'pro'pattern = r"E:\demos\files\reports\pro*"for item in glob.iglob(pattern, recursive=True): os.remove(item)Code language: Python (python)

Delete file whose name contains a specific letters

We can give a range of characters as the search string by enclosing them inside thesquare brackets ([]).

The following example will show how to delete files whose name contains characters between a-g.

import globimport os# search files like abc.txt, abd.txtpattern = r"E:\demos\files_demos\reports\[a-g]*.txt"for item in glob.iglob(pattern, recursive=True): os.remove(item)Code language: Python (python)

Deleting Files Matching a Pattern from all Subfolders

While the glob() function finds files inside a folder, it is possible to search for files inside the subfolders using the iglob() function which is similar to the glob() function.

The iglob() function returns iterator options with the list of files matching a pattern inside the folder and its subfolder.

We need to set the recursive flag to True when we search for the files in subdirectories. After the root folder name, we need to pass ** for searching inside the subdirectories.

import globimport os# Searching pattern inside folders and sub folders recursively# search all jpg filespattern = r"E:\demos\files\reports\**\*.jpg"for item in glob.iglob(pattern, recursive=True): # delete file print("Deleting:", item) os.remove(item)# Uncomment the below code check the remaining files# print(glob.glob(r"E:\demos\files_demos\reports\**\*.*", recursive=True))Code language: Python (python)

Output

Deleting: E:\demos\files\reports\profits.jpgDeleting: E:\demos\files\reports\revenue.jpg

Conclusion

Python provides several modules for removing files and directories.

To delete Files: –

  • Use os.remove() and os.unlink() functions to delete a single file
  • Use pathlib.Path.unlink() to delete a file if you use Python version > 3.4 and application runs on different operating systems.

To delete Directories

  • Use os.rmdir() or pathlib.Path.rmdir() to delete an empty directory
  • use the shutil.rmtree() to recursively delete a directory and all files from it.

Take due care before removing files or directories because all the above functions delete files and folders permanently.

Delete (Remove) Files and Directories in Python (2024)

FAQs

How do you delete a directory and files in it in Python? ›

to delete a directory:
  1. import shutil.
  2. # Specify the path of the directory to be deleted.
  3. directory_path = '/path/to/directory'
  4. # Check if the directory exists before attempting to delete it.
  5. if os.path. exists(directory_path):
  6. shutil. rmtree(directory_path)
  7. print(f"The directory {directory_path} has been deleted.")
  8. else:
Nov 22, 2023

How do I delete all subfolders and files in Python? ›

To delete a folder that has subfolders and files in it, you have to delete all the files first, then call os. rmdir() or path. rmdir() on the now empty folder. But instead of doing that, you can use the shutil module.

How do I empty and delete a directory in Python? ›

We can use functions from Python's built-in os module to delete files and empty folders.
  1. os. remove() will delete a file.
  2. os. rmdir() will delete an empty folder.
Apr 15, 2023

What is the best way to remove a file in Python? ›

One of the most straightforward ways to delete a file in Python is by using the os module's remove() function. This method is concise and well-suited for simple file deletion tasks.

How do you remove a file and directory? ›

The rm command in Linux removes files and directories. Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces. The different rm command options include: - f : Forces the removal of all files or directories.

How do I delete only directories in Python? ›

If you want to delete directories, use os. path. isdir() and shutil. rmtree() instead of os.

What is the command to delete subfolders and files? ›

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I delete all files in a directory and subdirectories? ›

The procedure to remove all files from a directory:
  1. Open the terminal application.
  2. To delete everything in a directory run: rm /path/to/dir/*
  3. To remove all sub-directories and files: rm -r /path/to/dir/*
Jan 19, 2024

How do you delete multiple files in Python? ›

Deleting Multiple Files in Python

remove() function. Alternatively, it is also possible to use a list comprehension to delete files to achieve the same result. In this method, the glob module is used to get a list of file paths that match a specified pattern using the glob. glob() function.

What is the command for deleting a directory? ›

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only . and ..) before you can remove it, and you must have write permission in its parent directory.

What command removes empty directory? ›

The rmdir or rm -d command is for removing empty directories, while the rm -r command deletes non-empty directories. Before removing a directory, you must know its name. To discover files and directories, use the ls command. To know the current working directory, use the pwd command.

How do I remove a directory key in Python? ›

To delete a key, you can use two options:
  1. Using del my_dict['key']
  2. Using my_dict. pop('key', None)

How do I delete a folder and file inside Python? ›

Delete a directory or file using Python
  1. Using os.remove()
  2. Using os.rmdir()
  3. Using shutil.rmtree()
  4. Using pathlib.Path(empty_dir_path).rmdir()
Oct 26, 2022

How do I clean unwanted data in Python? ›

Data Cleaning With Pandas
  1. Step 1: Import Dataset. To import the dataset, we use the read_csv() function of pandas and store it in the pandas DataFrame named data. ...
  2. Step 2: Merge Dataset. ...
  3. Step 3: Rebuild Missing Data. ...
  4. Step 4: Standardization and Normalization. ...
  5. Step 6: Verify and Enrich the Data. ...
  6. Step 7: Export Dataset.
Jun 22, 2024

What is the alternative to remove in Python? ›

Remove an Item from a List
  • Using Python remove()
  • Using Python del.
  • Using Python List comprehension.
  • Using Python pop()
  • Using Python discard()
  • Using Python filter()
  • Using Python List Slicing.
Dec 21, 2023

Which command is used to delete all files in a directory? ›

You use the rmdir command to remove an empty directory, while the rm command removes a directory and all its contents.

How do I close a file in Python? ›

Python File close() Method

The close() method closes an open file. You should always close your files, in some cases, due to buffering, changes made to a file may not show until you close the file.

How to make a directory in Python? ›

mkdir() in Python. The os. mkdir() method in Python creates a new directory (folder). It belongs to the os module, which provides a way to interact with the operating system.

Top Articles
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 6596

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.