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.
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.
- Delete a directory or file using Python
- Python Program to delete a file
- MongoDB python | Delete Data and Drop Collection
- Python | Write multiple files data to master file
- Python program to reverse the content of a file and store it in another file
- Create a GUI to convert CSV file into excel file using Python
- reStructuredText | .rst file to HTML file using Python for Documentations
- Commonly used file formats in Data Science
- Python - Get file id of windows file
- Sorting integer data from file and calculate execution time
- numpy.delete() in Python
- Python del to delete objects
- Python | Pandas Index.delete()
- Python MySQL - Delete Query
- DELETE method- Python requests
- Python | Delete elements in range
- Python | Pandas TimedeltaIndex.delete
- Python | Delete items from dictionary while iterating
- Delete Google Browser History using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.