-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathfile_handler.py
More file actions
37 lines (29 loc) · 1.14 KB
/
file_handler.py
File metadata and controls
37 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import pickle
from pprint import pprint
ITINERARY_FILE = "itineraries.bin"
# Used \\ because of this warning: https://stackoverflow.com/questions/52335970/how-to-fix-syntaxwarning-invalid-escape-sequence-in-python
current_directory = f"{os.getcwd()}\\{ITINERARY_FILE}"
print(current_directory)
def load_itineraries():
"""
Load tasks from a binary file using the pickle module.
Returns:
dictionary: A dictionary of Itinerary objects loaded from the binary file.
If the file does not exist, an empty dictionary is returned.
"""
if os.path.exists(current_directory):
with open(ITINERARY_FILE, "rb") as file:
return pickle.load(file)
return []
def save_itineraries(itineraries):
"""
Save a list of itineraries to a binary file using the pickle module.
Args:
itineraries (dict): A dictionary containing Itinerary objects to save.
Side Effects:
- Writes the serialized itinerary dictionary to ITINERARY_FILE.
- Overwrites the file if it already exists.
"""
with open(current_directory, "wb") as file:
pickle.dump(itineraries, file)