How can I delete a file or folder in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Python is a versatile programming language that supports a wide range of operations on files and directories. Deleting files and folders is a common task that can be performed easily in Python using built-in libraries. This article covers the technical details and provides examples to guide you through the process of deleting files and folders in Python.
Modules for File and Folder Deletion
Python provides several modules to handle file and directory operations:
- os: The
osmodule provides a way to interface with the underlying operating system. It includes functions to delete both files and folders. - shutil: The
shutilmodule provides high-level operations on files and collections of files. It is useful for managing a directory tree and can remove directories with their contents. - pathlib: Introduced in Python 3.4, the
pathlibmodule offers an object-oriented approach to filesystem paths. It can be used to delete files and directories.
Deleting Files
Using the os.remove() Function
To delete a file using the os module, you can use the os.remove() function. This function takes the file path as an argument and deletes the specified file.
Using the pathlib.Path.unlink() Method
For those who prefer an object-oriented approach, the pathlib module offers the Path.unlink() method.
Deleting Folders
Using the os.rmdir() Function
The os.rmdir() function deletes an empty directory. If the directory has contents, you'll need to use a different method, such as shutil.rmtree().
Using the shutil.rmtree() Function
To delete a directory and its contents, use shutil.rmtree().
Using the pathlib.Path.rmdir() Method
For an object-oriented approach to delete an empty directory:
Summary Table
| Operation | Method/Function | Supports Deleting | Key Module |
| File Deletion | os.remove(path) | Files | os |
| File Deletion | Path.unlink() | Files | pathlib |
| Empty Directory Deletion | os.rmdir(path) | Empty Folders | os |
| Non-Empty Directory | shutil.rmtree(path) | Folders + Content | shutil |
| Empty Directory Deletion | Path.rmdir() | Empty Folders | pathlib |
Error Handling
Error handling is crucial when performing file and directory operations. Key exceptions include:
FileNotFoundError: Raised when trying to delete a file or directory that does not exist.PermissionError: Raised when there is no permission to delete the file or directory.OSError: Raised byos.rmdir()when trying to delete a non-empty directory.
Additional Details
Permissions
Always ensure that your script has the necessary permissions to delete a file or directory. In Unix-like systems, this may involve using sudo or adjusting file permissions with chmod.
Cross-Platform Considerations
The functions provided in the os, shutil, and pathlib modules are generally cross-platform, but always perform tests on your specific environment to verify compatibility.
Safe Deletion
If you require safe deletion to prevent accidental data loss, consider implementing a confirmation step or moving files to a temporary directory for recovery.
Conclusion
Deleting files and folders using Python is straightforward with the use of the os, shutil, and pathlib modules. By understanding how to utilize these tools and handling exceptions properly, you gain better control over file system operations, making Python an efficient choice for scripting and automation tasks related to file management.

