#๐Ÿ”’ Why am I getting PermissionError here?

18 messages ยท Page 1 of 1 (latest)

dire moat
#
try:
    with open(str(os.path.dirname(os.path.abspath(__file__))).replace(__file__, "savedata.json")) as saveddata:
        savedata = json.load(saveddata)
        pass
    # TODO: FIX THIS ERROR PLEASE
except FileNotFoundError:
    functions.animated_print("Savedata was not found. If you already have a savedata.json file, copy-paste it here.")
    time.sleep(10)
    raise Exception("Program exited.")
# except PermissionError:
#     functions.animated_print("potatoes yum")
PS C:\Stuff> py "c:\Stuff\Programming\Python\LCP Enhanced\script.py"
Starting inspection... The program will automatically close after 10 seconds if failed the inspection.
Python interpreter version requirement passed (Required: 3.10~, Current: 3.13.0)
Dependencies (functions.py) passed.
External libraries passed.
Traceback (most recent call last):
  File "c:\Stuff\Programming\Python\LCP Enhanced\script.py", line 31, in <module>
    with open(str(os.path.dirname(os.path.abspath(__file__))).replace(__file__, "savedata.json")) as saveddata:
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'c:\\Stuff\\Programming\\Python\\LCP Enhanced'
PS C:\Stuff> 

What am I doing wrong here??

agile stirrupBOT
#

@dire moat

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dire moat
#

Why am I getting PermissionError here?

ornate forge
#

You're getting dirname(__file__). You can even see this in the exception message. Your .replace(__file__,"savedata.json") will not match anything. You could drop the dirname(). But better it to keep the dirname() and just use os.path.join(dirname(__file__),"savedata.json")

#

Print out all the intermediate values. Don't directly open the result of a complex expression. Compute the new file path, print it, then open it.

dire moat
#

what

#

sorry, i can't understand what you're saying

ornate forge
#

Don't open(long-expression-here).

Do:

savepath = long-expression-here
print("save to", savepath)
open(savepath,....)

If savepath is the wrong path, break the expression up into pieces. Print the pieces. See what is going on.

#

Pick this expression apart:
os.path.dirname(os.path.abspath(__file__))).replace(__file__, "savedata.json")
and see what each step produces.

#

And read the exception message carefully:
PermissionError: [Errno 13] Permission denied: 'c:\\Stuff\\Programming\\Python\\LCP Enhanced'

What does is say you were trying to do. Specificly.

dire moat
#

oh it worked

#

ty

#

oh wait i was stupid

#

i was trying to open a folder

#

๐Ÿ˜‚

#

!close

agile stirrupBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.