0

So I've been making a Dice Rolling app that takes 3 values from the user (how many dice are rolling, how many faces per die, and whether or not to add or subtract anything from the total), and I've been iterating various measures to make sure that all of the user input values won't cause a crash.

So I've verified that my class-based error checking is working, and that it does exactly what I want it to in reguards to handling exceptions, and now I'm just working on quality of life stuff.

So I have 3 situations that the user can mess up on the input, and for each case that catches an error I want to write it to a pre-existing file and continue so that a different module can read the results and show them to the user. Now, my struggle is the program isn't writing anything to the text file and the file is unreadable according to the system. I triple checked and confirmed that any relative references are correct, all syntax is good, and variable names and stuff matched. The class methods that check for errors work and return the values I want them to. My code is essentially something like:

# unpack the results of case A, B, and C (they are stored in a dict structured like {"is_err": true/false, "err_message": " " for no errors, "error message here" if there is}

err_repo = open("fileA.txt", "a")

exception_caught = False
err_repo.truncate(0) # make sure any previous data is erased
err_repo.write(" ") # makes sure file isn't totally empty for redundancies
if caseA["is_err"] == True:
     exception_caught = True #this is returned to the main program to say whether it calls a roll_dice function or not [the user enters a string, the program will convert to an int if exception_caught is False. Otherwise it will read the contents of fileA.txt
     err_repo.write(str(caseA["err_message"]))
if caseB["is_err"] == True:
     #same as caseA but replacing relevant variables
if caseC["is_err"] == True:
     #same as caseA but replacing relevant variables

     retval = err_repo.read()
     return {"exception_caught": exception_caught, "error_message" = retval}

So what's happening is a little goofy. The file is being written upon, but when I call fileA.read() I get an error saying the file is unreadable. Is there anything extra I need to know about writing to things, please let me know. I've made sure that anything being written to the file is a normal string, not any funky symbols or weirdness. Any help is appreciated here.

Thanks in advance.

PS the error text is "io.UnsupportedOperation: not readable"

Coder man
  • 25
  • 7
  • You show where you open the file, but not where you close it. Is that step just missing from the example, or missing from the code itself? Might be a good use case for the `with` context manager – G. Anderson Oct 07 '22 at 18:38
  • 1
    Does the file exist ? If not a+ might be needed. https://stackoverflow.com/a/52912994/4168707 – kgkmeekg Oct 07 '22 at 18:38
  • You opened the file in `a` mode. That only allows writing, not reading. Use `a+` if you want to do both. – Barmar Oct 07 '22 at 18:59
  • But you'll need to seek back in the file to read something after you write. Otherwise you'll be at the end of the file and there's nothing to read. – Barmar Oct 07 '22 at 19:00

0 Answers0