adding basic functionality to create, write, read save file data #7

Merged
ch0ccyra1n merged 2 commits from issue-2 into main 2024-09-09 20:31:45 +00:00
Showing only changes of commit d64b9dccf0 - Show all commits

53
save.py Normal file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env python
import yaml
from pathlib import Path
## create save file if it doesn't exist
def create_save_file():
path_to_file = 'save.yaml'
path = Path(path_to_file)
if path.is_file():
print(f'The file {path_to_file} exists, reading file')
f = open('save.yaml', 'r')
f.close()
else:
print(f'The file {path_to_file} does not exist, creating file')
f = open('save.yaml', 'x')
print(path)
f.close()
create_save_file()
# write to file example
def write_save_file():
prime_numbers = """
- 2
- 3
- 5
- 7
- 11
- 13
- 17
- 19
"""
primes = yaml.safe_load(prime_numbers)
with open('save.yaml', 'w') as file:
yaml.dump(primes, file)
# print for debugging
print(open('save.yaml').read())
file.close()
write_save_file()
# open file, read, save contents to variable
def open_save_file():
with open('save.yaml', 'r') as file:
save_file = yaml.safe_load(file)
# print for debugging
print(open('save.yaml').read())
# close file
file.close()
open_save_file()