oni-engine/save.py

54 lines
1.2 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()
# write to file example
def write_save_file(data):
with open('save.yaml', 'w') as file:
yaml.dump(data, file)
# print for debugging
print(open('save.yaml').read())
file.close()
# 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()
if __name__ == '__main__':
print("Beginning Save File Test...")
prime_numbers = """
- 2
- 3
- 5
- 7
- 11
- 13
- 17
- 19
"""
primes = yaml.safe_load(prime_numbers)
create_save_file()
write_save_file(primes)
open_save_file()