How to delete data from file in Python
When we no longer require data, we usually prefer to erase or delete it so that space can be used up by other data that is important to us. Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files.
Refer to the below articles to get the idea about file handling in Python.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Here, we will be learning different approaches that are used while deleting data from the file in Python.
Method 1: When the entire data along with the file, it is in, has to be deleted!
os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method. os.rmdir() can be used to remove directory.
Note: For more information, refer to Python | os.remove() method
Method 2: When the entire data has to be deleted but not the file it is in !
Truncate() method truncate the file’s size. If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed. Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content. To truncate the file, you can open the file in append mode or write mode.
Note: For more information, refer to Python File truncate() Method.
Method 3: Delete a particular data from the file
This can be done by following ways:
- Open file in read mode, get all the data from the file. Reopen the file again in write mode and write all data back, except the data to be deleted
- Rewrite file in a new file except for the data we want to delete. faster)
Let us see the first one.