May 2018
Beginner to intermediate
452 pages
11h 26m
English
Start a new method called get_all_records():
def get_all_records(self): if not os.path.exists(self.filename): return []
The first thing we've done is check if the model's file exists yet. Remember that when our application starts, it generates a default filename pointing to a file that likely doesn't exist yet, so get_all_records() will need to handle this situation gracefully. It makes sense to return an empty list in this case, since there's no data if the file doesn't exist.
If the file does exist, let's open it in read-only mode and get all the records:
with open(self.filename, 'r') as fh:
csvreader = csv.DictReader(fh)
records = list(csvreader)
While not terribly efficient, pulling the entire file into ...