How to Delete a File in Python
If you want to delete a file in Python, the most common way is to use os.remove().
For beginners, the safest approach is:
- store the file path in a variable
- check whether the file exists
- delete it only if it is really there
This helps you avoid common errors like FileNotFoundError.
Quick answer #
import os
file_path = "notes.txt"
if os.path.exists(file_path):
os.remove(file_path)
print("File deleted")
else:
print("File does not exist")
Use os.remove() to delete a file. Check that the path exists first to avoid a FileNotFoundError.
What this page helps you do #
- Delete a single file with Python
- Use the correct function for files, not folders
- Check whether a file exists before deleting it
- Understand common errors you may see
Delete a file with os.remove() #
To delete a file, import the os module and pass the file path to os.remove().
import os
os.remove("notes.txt")
print("File deleted")
What this code does #
import osgives you access to file system toolsos.remove("notes.txt")deletes the file"notes.txt"is the path to the file
You can use:
- a simple file name like
"notes.txt" - a relative path like
"data/report.csv" - an absolute path like
"/Users/name/Documents/notes.txt"
This removes the file from the file system.
If you are new to file paths, see working with file paths in Python.
Check if the file exists first #
A beginner-friendly way to delete a file is to check for it first.
import os
file_path = "notes.txt"
if os.path.exists(file_path):
os.remove(file_path)
print("File deleted")
else:
print("File does not exist")
Why this helps #
- It avoids
FileNotFoundError - It makes your script easier to understand
- It is useful when the file may or may not exist
If you want more detail, see how to check if a file exists in Python and the os.path.exists() function explained.
Use the right path #
A wrong path is one of the most common reasons file deletion fails.
Relative path #
A relative path starts from the folder your script is currently using.
import os
file_path = "notes.txt"
print(os.getcwd()) # shows the current working folder
if os.path.exists(file_path):
os.remove(file_path)
Absolute path #
An absolute path gives the full location of the file.
import os
file_path = "/home/user/project/notes.txt"
if os.path.exists(file_path):
os.remove(file_path)
If you are not sure what path your code is using #
Print the path before deleting:
file_path = "notes.txt"
print(file_path)
You can also print the current working folder:
import os
print(os.getcwd())
If your file path problems are causing errors, see how to understand file paths in Python and how to fix FileNotFoundError: [Errno 2] No such file or directory.
Deleting with pathlib #
You can also delete files with pathlib, which many beginners find easier to read.
from pathlib import Path
file_path = Path("notes.txt")
if file_path.exists():
file_path.unlink()
print("File deleted")
else:
print("File does not exist")
Why use pathlib? #
- The code often reads more clearly
Pathobjects are convenient for file work- It works well when you do many path-related tasks in one script
Path.unlink() is for files, not directories.
Both styles are fine:
os.remove(path)Path(path).unlink()
Choose one style and stay consistent in your script.
Common errors when deleting files #
Here are the main errors you may see.
FileNotFoundError #
This usually means:
- the path is wrong
- the file was already deleted
- your script is running in a different folder than you expected
Example:
import os
os.remove("missing_file.txt")
Possible fix:
import os
file_path = "missing_file.txt"
if os.path.exists(file_path):
os.remove(file_path)
else:
print("File does not exist")
For a full fix guide, see FileNotFoundError: [Errno 2] No such file or directory.
PermissionError #
This means your program does not have permission to delete the file.
Example:
import os
os.remove("protected_file.txt")
This can happen if:
- the file belongs to another user
- the file is protected by the operating system
- another program is locking the file
See how to fix PermissionError: [Errno 13] Permission denied.
IsADirectoryError #
This happens when you try to delete a folder with a file function.
import os
os.remove("my_folder")
os.remove() is for files only.
See how to fix IsADirectoryError: [Errno 21] Is a directory if that happens.
OSError #
This is a broader file system error. It can happen for different system-level reasons.
A simple way to handle deletion errors is to use try and except:
import os
file_path = "notes.txt"
try:
os.remove(file_path)
print("File deleted")
except FileNotFoundError:
print("File does not exist")
except PermissionError:
print("You do not have permission to delete this file")
except OSError as error:
print("A file system error happened:", error)
Simple safety tips #
When deleting files, a few small checks can prevent bigger mistakes.
- Print the file path before deleting important files
- Test your code with a sample file first
- Do not delete files directly from unchecked user input
- Use
try-exceptif your script should keep running after an error
Example:
import os
file_path = "test_file.txt"
print("About to delete:", file_path)
try:
if os.path.isfile(file_path):
os.remove(file_path)
print("File deleted")
else:
print("That path is not a file")
except PermissionError:
print("Permission denied")
Common mistakes #
These are common causes of problems when deleting files:
- Using the wrong file path
- Trying to delete a folder instead of a file
- Deleting a file that does not exist
- Not having permission to delete the file
- Confusing relative paths with absolute paths
Useful debugging checks:
print(file_path)
import os
print(os.getcwd())
import os
print(os.path.exists(file_path))
import os
print(os.path.isfile(file_path))
import os
print(os.listdir())
These commands help you answer basic questions:
- What path is my code using?
- Which folder is my script running in?
- Does the path exist?
- Is it really a file?
- What files are in the current folder?
FAQ #
What is the easiest way to delete a file in Python? #
Use os.remove(path). For a safer beginner approach, check os.path.exists(path) first.
How do I delete a file only if it exists? #
Use an if statement with os.path.exists(path), then call os.remove(path).
import os
file_path = "notes.txt"
if os.path.exists(file_path):
os.remove(file_path)
Can os.remove() delete a folder? #
No. It is for files. Folders need a different function. If you need to create folders, see how to create a directory in Python.
Why do I get FileNotFoundError when deleting a file? #
Usually:
- the file path is wrong
- the file was already deleted
- your script is running in a different folder than you expect
Should I use os.remove() or pathlib? #
Both work.
os.remove()is common and simplepathlib.Path.unlink()is also clear and beginner-friendly