54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
![]() |
#!/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()
|