Beginner Level
Intermediate Level
Advanced Level
Introduction
In this tutorial, we will explore various methods to delete files and directories using Python's built-in functions and third-party libraries. Whether you are a beginner or an experienced developer, this tutorial will equip you with the necessary knowledge to manage your file system efficiently. So, let's dive in and learn how to delete files and directories in Python.
Table of Contents :
- Different methods of deleting files in python
- delete a File in python using os.remove()
- delete a file in python using rmtree()
- remove file using os.unlink()
- Using the Pathlib Module to remove file
- Different scenarios of deleting files in python
- Delete all files from a directory
- deleting files that match a pattern
- delete 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
- Path of the file to delete
- Remove files using relative path
- Remove files using absolute path
- How to check if file exist before deleting it
- Deleting directories in python
- delete an empty directory using rmdir()
- delete a non-empty directory using rmtree()
Different methods of deleting files in python
How to delete a File in python using os.remove() function
- Use the
os.remove()
function to delete a file from a directory using the file's path. - Use the
os.remove()
method to delete a file from a directory. - If the file does not exist, it will raise a
FileNotFoundError
. - Code Sample :
import os
os.remove("/path/to/file")
How to delete a file in python using rmtree() function
- Use the
shutil
module'srmtree()
function to delete a directory and all its contents. - Code Sample :
import shutil
shutil.rmtree("/path/to/directory")
How to remove file using os.unlink() Method
- Use the
os.unlink()
method to delete a file from a directory. - Code Sample :
import os
os.unlink("/path/to/file")
Using the Pathlib Module to remove file
- Use the
pathlib
module to remove a file from a directory. - Code Sample :
from pathlib import Path
file = Path("/path/to/file")
file.unlink()
Different scenarios of deleting files in python
Delete all files from a directory
- Use the
os
module to delete all files from a directory. - Code Sample :
import os
for file in os.listdir("/path/to/directory"):
os.remove(os.path.join("/path/to/directory", file))
How to deleting files that match a pattern
- Use the
glob
module to delete files from a directory that match a specific pattern. - Code Sample :
import glob
import os
for file in glob.glob("/path/to/directory/*.txt"):
os.remove(file)
How to delete files with specific extension
- Use the
os
module to delete files from a directory with a specific extension. - Code Sample :
import os
for file in os.listdir("/path/to/directory"):
if file.endswith(".txt"):
os.remove(os.path.join("/path/to/directory", file))
How to delete file whose name starts with specific string
- Use the
os
module to delete files from a directory whose name starts with a specific string. - Code Sample :
import os
for file in os.listdir("/path/to/directory"):
if file.startswith("text"):
os.remove(os.path.join("/path/to/directory", file))
How to delete file whose name contains a specific letters
- Use the
os
module to delete files from a directory whose name contains specific letters. - Code Sample :
import os
for file in os.listdir("/path/to/directory"):
if "sample" in file:
os.remove(os.path.join("/path/to/directory", file))
How to deleting files matching a pattern from all subfolders
- Use the
os.walk()
method to delete files from all subfolders of a directory that match a specific pattern. - Code Sample :
import os
for root, dirs, files in os.walk("/path/to/directory"):
for file in files:
if file.endswith(".txt"):
os.remove(os.path.join(root, file))
Path of the file to delete
Remove files using relative path
- Use the relative path to delete a file from a directory.
- Code Sample :
import os
os.remove("file_name")
Remove files using absolute path
- Use the absolute path to delete a file from a directory.
- Code Sample :
import os
os.remove("/path/to/file")
How to check if file exist before deleting it
- Use the
os.path.exists()
method to check if a file exists before deleting it from a directory. - Code Sample :
import os
if os.path.exists("/path/to/file"):
os.remove("/path/to/file")
else:
print("File does not exist")
Deleting directories in python
How to delete an empty directory using rmdir()
- Use the
os
module'srmdir()
method to delete an empty directory from a directory. - Code Sample :
import os
os.rmdir("/path/to/empty_directory")
How to delete a non-empty directory using rmtree()
- Use the
shutil
module'srmtree()
method to delete a non-empty directory from a directory. - Code Sample :
import shutil
shutil.rmtree("/path/to/non_empty_directory")
Prev. Tutorial : List of files in a Dir.
Next Tutorial : Copy Files and Dir.