Re: Assistance with my settings class (Python)

From what I can see, you are trying to read a file as plain text, when actually you've written it as binary
what you'd want to use when reading is the "rb" mode.
And the issue I can also see, is that you are using a lot of try and except blocks, without actually specifing which exception to catch. Therefore adding these many try except blocks will just prevent you  from actually finding out what's wrong.
As a good rule of thumb, always catch the exceptions you need.
Using just except without specifying any exception, is going to hide whatever other error you are getting.
Also, when dealing with  file handles, you could also consider using the finally clause to close it. Finally always executes whether the try operation was successful or not, and you do always want to close the file handle
So after my nonsensical talk, here should be a working class

# I'll pretend that the modules have been imported
class SaveData:
    def __init__(self, filename, key):
        self.d = {}
        self.filename = filename
        self.key = key

    def load(self):
        sd = None # let's keep the future  handle out of the try scope, so we can use it later
        dicdata = ""
        try:
            sd = open(self.filename, "rb")
            dicdata = sd.read()
        except FileNotFoundError: # exception raised when file not found
            pass
        finally:
            if sd:
                sd.close()

        # here we can decide to decrypt the data if a key is present
        if self.key != "":
            dicdata = data.decript(dicdata, self.key)

        # finally, let's load json
        self.d = json.loads(dicdata)

in the future, you might want to explore also utilizing the with statement, when dealing with IO operations, it'll make your life much better in most cases
I hope it made some sense. And if not feel free to ask.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : tunmi13 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Turret via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : tunmi13 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Turret via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Turret via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : stewie via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : tunmi13 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : tunmi13 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Turret via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : bhanuponguru via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : tunmi13 via Audiogames-reflector

Reply via email to