+ 1
[Solved] Getting a permission error during a practice
I am going through the Python Intermediate course, and I am currently doing the practice coding for "Filling Up With Numbers", but no matter what code I use, I always get this error: "Traceback (most recent call last): File "/usercode/file0.py", line 3, in <module> file = open("numbers.txt", "w+") PermissionError: [Errno 13] Permission denied: 'numbers.txt'" Is this a bug, or is there a way to fix this?
26 Réponses
+ 2
Benjamin Rogers ,
By the way, Sololearn teaches a horrible practice of creating new files with 'w' mode, risking destroying the contents of any existing file accidentally having the same name.
There's already a perfectly good mode whose purpose is specifically for safely creating new files: 'x'
You can't hurt the virtual environment here, but it's good to build a habit of using 'x' anyway, so when you manage files at home, or worse, on the job, you don't blow it.
+ 2
Wong Hei Ming Rain Bob_Li Solo
I found out why that error is popping up, and I've sent sololearn an email with screenshots of my discovery. Hopefully, they will fix it soon.
+ 2
So, I ran this code both in playground and in that exercise:
from pathlib import Path
curr = Path.cwd()
for item in curr.iterdir():
print(f" ' {item} ' ")
And you can see that in playground you are running code from the /usercode directory, but in that exercise, you are running from the root "/" directory.
+ 2
Solo
That works if you already completed it, but it didn't work for me as I hadn't completed it before.
I just ran through it again with my original code, and I can confirm that the issue has been resolved.
+ 1
Yes, I also get an error in the test, there is no access to the file, although the code works well in the sandbox.
+ 1
It is strange, it seems something in the backend is changed.
The old code doesn't pass the pass anymore.
However, if I change the code a bit it works.
# change the switch to 'r' to open the file
file = open("/usercode/files/numbers.txt", "w+")
+ 1
can confirm.
someone changed something and the file cannot be accessed in the practice.
maybe they accidentally changed it to pro only internally?
+ 1
Bob_Li
Changing the following codes:
file = open("numbers.txt", "w+")
f = open("numbers.txt", "r")
into this:
file = open("/usercode/files/numbers.txt", "w+")
f = open("/usercode/files/numbers.txt", "r")
It gets pass the permission problem.
Tested on PC and the apps.
But there is definitely something wrong with the backend.
The code is not syntax highlight as used to be when you open with PC.
All text is white just like pain text, but the codes run.
Benjamin Rogers
I think you should report this bug to SL along with the findings.
At one occasion they fixed a bug with a hard evidence in my experience.
+ 1
Bob_Li
I think they changed the folder permission, since the code is making a new file on the fly...
+ 1
Wong Hei Ming ,
Yes, and that's what 'w' should be restricted to. You should only use 'w' when you know the file already exists and the user has chosen to overwrite it.
'x' should be used to safely create a new file, because it automatically aborts if you've accidentally chosen a name that already exists.
+ 1
Eden Wheeler
The issue isn't with my system, it's with the practice codes in Sololearn's system when going through the learning modules.
Edit: More specifically, the module I have listed. The other practice code segments seem to work fine.
+ 1
Benjamin Rogers thank you for sharing your research, but it was already clear from Wong Hei Ming recommendation to change the file name to /usercode/files/numbers.txt the path of which is used in the Playground, which is actually what I did once again after completing this task, since I had already passed it before.
By the way, I just tried to go through it with this address again and couldn't, I restored the previous name numbers.txt and everything worked without any problems.
So everything has been restored and this problem is no longer relevant.
Apparently, it really happened that the developers of Sololearn were working with this file at the moment...😎
+ 1
Benjamin Rogers in this case, you can safely mark [Solved] in the title of the question...😎
0
# Benjamin Rogers ,
# Do you need to specify a path?
# Maybe you can troubleshoot
# with a known permanent file
# just to see if any file access
# works.
# I just confirmed this snippet
# works in the playground
# outside any course. It prints
# Harry Potter
# etc.
with open("/usercode/files/books.txt") as f:
print(f.read())
0
Rain,
Yeah, in the playground it works. In REPL, it works. In my IDE, it works... but for some reason, it won't work during that practice... and I can't move onto the next lesson without it. I've tried every variation of file handling, I even replaced file = open() with the `with` statement, same error.
0
Rain, I don't know why you said SL taught us wrongly about opening file in multiple places.
These are the text I copied from the lessons, it clearly says 'w' switch REWRITE the contents of a file.
Opening Files
-------------
You can specify the mode used to open a file by applying a second argument to the open function.
Sending "r" means open in read mode, which is the default.
Sending "w" means write mode, for rewriting the contents of a file.
Sending "a" means append mode, for adding new content to the end of the file.
Adding "b" to a mode opens it in binary mode, which is used for non-text files (such as image and sound files).
Writing Files
-------------
In case the file already exists, its entire content will be replaced when you open it in write mode using "w".
If you want to add content to an existing file, you can open it using the "a" mode, which stand for "append"
0
Wong Hei Ming ,
Yes. They said 'w' clears the file contents and yet still used it in all the examples of opening a new file. They didn't even mention 'x', which is exactly for that purpose. And neither did you.
0
Rain
'w' has it usefulness.
For example, you want to save a game and overwrite the existing save file.
Another example is writing a document, where we press Ctrl S to save the file with the same file name.
0
But the current problem is a permission-level access restriction. This feels like someone in the Sololearn IT crew changed something they shouldn't have in this part of the codebase.
0
Wong Hei Ming
so maybe they changed the code's file location to somewhere where it does not have access permission...
yes, specifying the alternate file path where the user have access permission is a workaround.👍