Answer: To delete a file in Python you can use the os.remove() function.
Deleting files and folders is an essential operation to manage the files. Python provides multiple ways to remove files and folders efficiently. This blog explores the methods to delete files and folders in Python.
Table of Contents:
Methods to Delete a File or Folder in Python
Python provides various methods to delete a file or folder in Python. Following are some of these methods explained with examples:
Method 1: Using os.remove() to Delete a File in Python
The os.remove() function deletes a specified file from the file system.
Example:
Output:
Explanation:
The os.remove(file_path) deletes the specified file and print() confirms that the file was deleted.
Method 2: Using os.rmdir() to Delete an Empty Folder in Python
The os.rmdir() function removes an empty folder. It can delete only those folders which do not contain any files or subfolders.
Example:
Output:
Explanation:
The os.rmdir(folder_path) removes the empty folder print() confirming the folder was deleted.
Method 3: Using the shutil Module to Delete a Folder and its Contents in Python
The shutil module provides the rmtree() method to delete a folder along with all its files and subfolders.
Example:
Output:
Explanation:
The shutil.rmtree(folder_path) deletes the folder along with all the files and folders inside it and the print() confirms the operation was successful.
Method 4: Using path.unlink() to Delete a File using Python
The path.unlink() function from pathlib deletes a file present in the folder.
Example:
Output:
Explanation:
The file_path.unlink() deletes the specified file present inside the folder by only specifying the file name and print() confirms the deletion.
Method 5: Using path.rmdir() to Delete an Empty Folder in Python
The path.rmdir() function deletes an empty folder using the pathlib. This function works only for the folders which do not contain any files.
Example:
Output:
Explanation:
The folder_path.rmdir() deletes the empty folder and the print() confirms the deletion of the folder
Conclusion
All these methods allow efficient file and folder deletion in Python. Use os.remove() for files and os.rmdir() for empty folders. The shutil.rmtree() is the optimal choice for deleting the entire folder along with the contents. Understanding these approaches allows you to process the text data effectively.
Below is a collection of blogs that discuss file and module operations in Python–
Rename DataFrame Columns — Renaming columns in a pandas DataFrame is the focus of this article, with clear examples provided.
Current Script Name — You’ll find examples in this article that explain how to get the name of the currently running script in Python.
Read File Line by Line — This tutorial demonstrates how to read a file line by line in Python using practical code examples.
Get Current Date and Time — Explore how to retrieve the current system date and time in Python, supported by working examples.
Import Module from Different Directory — This article details the process of importing modules from different directories in Python using examples.
Get Row Count of DataFrame — Understand how to determine the number of rows in a DataFrame with Python, explained using examples.
Iterate over Dictionary — Here, you’ll see how to iterate over dictionary items in Python with examples that cover various use cases.
String Lowercase — Converting strings to lowercase in Python is made simple in this article, with code samples included.
NumPy where — This guide covers the use of the NumPy where function in Python, complete with real-world examples.