From 8441c6acfb4d25324946bbd37ace75ee4b7524e7 Mon Sep 17 00:00:00 2001 From: Noelle Leigh Date: Sun, 1 Jun 2025 21:45:39 -0400 Subject: [PATCH] Use pathlib in test_user_car --- tilde-train.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tilde-train.py b/tilde-train.py index be654ec..4cc6226 100755 --- a/tilde-train.py +++ b/tilde-train.py @@ -37,12 +37,12 @@ from random import shuffle ## allowing us to randomize selection of cars. import glob ## allowing us to search the file system for .choochoo files. import sys ## so we can read command line arguments. -import getpass ## so we can get the user's username import curses from signal import signal, SIGINT import time ## allowing the loop steps of train animation to be slowed import string ## for input validation from inspect import cleandoc +from pathlib import Path traincarFN = ".choochoo" max_x = 35 ## max length of train car. @@ -122,16 +122,15 @@ def print_help(): def test_user_car(): - username = getpass.getuser() - fname = "/home/" + username + "/" + traincarFN + fname = Path.home() / traincarFN try: - myfile = open(fname, 'r') - except: - print("ERROR: Couldn't open " + fname) - print("Either it doesn't exist, or is not readble by the tilde.train script.") - sys.exit(1) + choochoo_string = fname.read_text("utf-8") + except OSError as err: + raise OSError( + f"Couldn't open {fname}\n" + "Either it doesn't exist, or is not readble by the tilde.train script." + ) from err - choochoo_string = myfile.read() choochoo_list = choochoo_string.split("\n") car = "\n".join(choochoo_list) @@ -177,17 +176,14 @@ def test_user_car(): if train_height > max_y+1: print("FAIL. Your train car is too tall.") print("It should be no taller than " + str(max_y) + " lines in height.") - myfile.close() sys.exit(1) if train_length > max_x: print("FAIL. Your train car is too long.") print("It should be no longer than " + str(max_x) + " characters in length.") - myfile.close() sys.exit(1) print("PASS. Your train car will work on the tilde.town tracks! :)") - myfile.close() sys.exit() def link_car(car):