Skip to content

Commit 04d39dc

Browse files
committed
enhance file saving with pathlib and better error handling
1 parent 91bca74 commit 04d39dc

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

games/Rock-Paper-Scissor/Rock-Paper-Scissor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import random
2-
import os
32
from pathlib import Path
43

54
RESULTS_FILE = Path(__file__).parent / "game_results.txt"
5+
RESULTS_FILE.parent.mkdir(parents=True, exist_ok=True)
66

77

88
def parse_results():
@@ -210,11 +210,11 @@ def main():
210210
f"(User-Computer), Rounds: {rounds_played}\n"
211211
)
212212
try:
213-
with open(RESULTS_FILE, "a", encoding="utf-8") as f:
213+
with RESULTS_FILE.open("a", encoding="utf-8") as f:
214214
f.write(result_string)
215215
print("Game results saved successfully.")
216-
except IOError:
217-
print("Error: Could not save game results to file.")
216+
except OSError as e:
217+
print(f"Error: Could not save game results: {e}")
218218
break
219219

220220

0 commit comments

Comments
 (0)