How to Delete a Record From a File With Python
Max O'Didily
How to Delete a Record From a File With Python
Greetings, today we shall be covering how to delete a line or record from a file using Python.This tutorial will show you how to delete a line or record from a csv/text file by specifying a search term and deleting the line/record if it matches the search term criteria.
We can acomplish this by specifying a field of a record we want a record we want to delete to contain. We specify the value and which field of a record that value should exist in.
We loop through evey line/record of the file. We split the record into an array, where each field has its own element. We then check if the current record contains the search term we specified in the correct field. If there is a match, we don't do anything. If there isn't a match, we write the current record we are reading to a temp file.
Once we have looped through the entire file, we delete the file we were reading and then rename the temp file to whatever the original file was called.
This creates the illusion of deleting a record from a file but what we really did was write every record we want to keep to a new file and renamed it.
Thanks for watching this deleting a record from a file using Python tutorial.
Subscribe to keep notified when I upload?: https://tinyurl.com/SubMaxODidily
How to Delete a Record From a File With Python ... https://www.youtube.com/watch?v=Q2Svk8oeqOU
21874398 Bytes