How to Delete a File in Python

By
Milica Dancuk
Published:
June 12, 2025
Topics:

Deleting a file is a common file-handling task in Python. Whether you are cleaning up temporary data or managing an application, knowing how to delete files from the system is essential.

Python offers multiple ways to find and delete files from the system using different libraries. Choose a method that best suits your use case and coding style.

This guide shows how to delete files in Python.

How to Delete a File in Python

Prerequisites

  • Python 3 installed and configured.
  • An IDE or code editor to write code.
  • A way to run the code (terminal or IDE).

How to Check if a File Exists?

Before deleting a file, check whether it exists. Use the os.path.exists() function in a conditional statement:

import os

file_path = "file.txt"

if os.path.exists(file_path):
  print("The file exists.")
else:
  print("The file does not exist.")
Check if a file exists output

The function returns True if the path exists, whether it is a file or a directory. To exclude directories, symlinks, and similar, use the os.path.isfile() function instead:

import os

file_path = "file.txt"

if os.path.isfile(file_path):
  print("The file exists.")
else:
  print("The file does not exist.")
Check if a file exists with isfile() output

Use this method to target regular files only.

How to Delete a File in Python?

Python offers several ways to delete a file, depending on your use case and coding context. The sections below explain and demonstrate the most common methods.

Delete a File in Python via os.remove()

The os.remove() function deletes a single file. Run the example below:

import os

file_path = "file.txt"

if os.path.exists(file_path):
  os.remove(file_path)
  print("File deleted.")
else:
  print("File not found.")
Delete a file with os.remove() output

The code checks if the file exists and deletes it if it does. The check is necessary since os.remove() raises an error if the file does not exist.

Delete a File in Python via send2trash Module

To move a file to the recycle bin, instead of permanently deleting it, use the send2trash module. Try the example below:

import os
from send2trash import send2trash

file_path = "file.txt"
if os.path.exists(file_path):
  send2trash(file_path)
  print("Sent to recycle bin.")
else:
  print("File not found.)
Python send2trash delete file recycle bin output

This method is safer because the files can be recovered from the recycle bin.

Note: The module is not available by default. Install it with:
sudo apt install python3-send2trash.

Delete a File in Python via unlink()

Use the unlink() method from the pathlib module to delete files in a readable way:

from pathlib import Path

file_path = Path("file.txt")

if file_path.is_file():
  file_path.unlink()
  print("File deleted.")
else:
  print("File not found.")
Python delete file using unlink output

The method is equivalent to os.remove(), but it uses an object-oriented approach to accessing file paths.

How to Delete Multiple Files in Python

To delete multiple files, use a loop, glob patterns, or list comprehension. The sections below explain these methods through examples.

Using a Loop with os.remove()

Use a for loop to iterate through a list of multiple files:

import os

files_list = ["file1.txt", "file2.txt", "file3.txt"]

for file_path in files_list:
  if os.path.exists(file_path):
    os.remove(file_path)
    print(f"Deleted {file_path}")
Python delete multiple files for loop output

The loop iterates through the list and deletes each file it finds.

Using glob to Match Patterns

Use the glob module to match a specific pattern. For example, to find and delete all .txt files in the current directory, see the following code:

import os
import glob

for file_path in glob.glob("*.txt"):
  if os.path.isfile(file_path):
    os.remove(file_path)
    print(f"Deleted {file_path}") 
Glob match multiple files and delete output

Use this method to delete multiple files with the same extension or with a similar naming convention (logs, temp files, or similar).

Using List Comprehension

List comprehension is another quick way to delete multiple files. See the example below:

import os

[os.remove(file_path) for file_path in ["file1.txt", file2.txt", "file3.txt"] if os.path.exists(file_path)]

The code does not produce an output. This method is concise but should be used sparingly, as list comprehensions are less readable for complex operations.

Conclusion

This guide explained how to delete one or more files in Python. Use a method that is most convenient and easy to use.

Next, see how to change the working directory in Python.

Was this article helpful?
YesNo