Python Delete Files
In this page:
import os
if os.path.exists("file_name"):
os.remove("file_name")
Deleting a File with os.remove()
os.remove(path) permanently deletes the file at the given path -- there is no built-in undo, so once a file is removed this way, it is genuinely gone (aside from whatever operating-system-level recovery tools might exist outside Python entirely).
Example: Deleting a File with os.remove()
import os
with open("temp.txt", "w") as f:
f.write("data")
os.remove("temp.txt") # permanently deletes the file, no undo
print(os.path.exists("temp.txt"))
Checking If a File Exists Before Deleting
os.path.exists(path) returns True if a file or folder exists at that path, and checking it before calling os.remove() avoids a FileNotFoundError crash on a file that was already deleted, never created, or misspelled.
Example: Checking If a File Exists Before Deleting
import os
if os.path.exists("missing.txt"): # avoids a crash if the file isn't there
os.remove("missing.txt")
else:
print("File does not exist")
Deleting an Empty Folder
os.rmdir(path) removes a folder, but only if it is completely empty -- calling it on a folder that still contains files or subfolders raises an OSError, a safety measure that prevents accidentally deleting a folder full of content with a single, simple call.
Example: Deleting an Empty Folder
import os
os.mkdir("empty_dir")
os.rmdir("empty_dir") # only works because the folder is empty
print(os.path.exists("empty_dir"))
Deleting a Folder and Everything Inside It
shutil.rmtree(path) removes an entire folder tree at once, including every file and subfolder inside it, regardless of how much content it holds -- far more powerful (and far more dangerous) than os.rmdir(), since it never asks for confirmation before deleting everything.
Example: Deleting a Folder and Everything Inside It
import os
import shutil
os.makedirs("full_dir/sub", exist_ok=True)
with open("full_dir/file.txt", "w") as f:
f.write("data")
shutil.rmtree("full_dir") # deletes the whole folder tree, files and all
print(os.path.exists("full_dir"))
Safer Deletion Patterns
For genuinely important data, a "soft delete" pattern (renaming or moving a file to a designated trash/archive folder instead of truly deleting it) offers a safety net that immediate, permanent deletion does not -- letting a mistake be undone by simply moving the file back.
Example: Safer Deletion Patterns
import os
import shutil
os.makedirs("archive", exist_ok=True)
with open("important.txt", "w") as f:
f.write("data")
shutil.move("important.txt", "archive/important.txt") # "soft delete" by moving instead of removing
print(os.path.exists("archive/important.txt"))
- Calling os.remove() on a file that does not exist, which raises a FileNotFoundError -- checking os.path.exists() first, or catching the exception, avoids an unhandled crash.
- Using os.remove() on a directory (rather than a file), which raises an IsADirectoryError -- directories need os.rmdir() or shutil.rmtree() instead.
- Deleting a file without any confirmation or backup in a script handling user data, when a mistake in the delete logic could destroy something irreplaceable.
- os.remove(path) deletes a single file, raising FileNotFoundError if it does not exist.
- os.path.exists(path) checks whether a file or folder exists before attempting to delete it, avoiding an unhandled exception.
- os.rmdir(path) removes an empty folder, while shutil.rmtree(path) removes a folder and everything inside it, even if not empty.
os.remove(), os.rmdir(), and shutil.rmtree() are part of Python's standard library and work identically across every Python 3 version and operating system.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: