Skip to content

Commit 8543888

Browse files
Merge pull request #1585 from kumudasrip/fix/result-file-destination
Improve game results persistence and file I/O handling
2 parents 26462bf + 04d39dc commit 8543888

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)