0

Why are stat_modes of the logfile.txt and file0.py are the same?

I'm using os module of python and I see several files, such as file0.py and logfile.txt. now I attempt to unlink() each one of these files, the file0.py can be successfully removed, but when I unlink the logfile.txt, there raises such an error : "stat : cannot statx '/usercode/logfile.txt' : No such a file or directory. mv : cannot stat '/usercode/logfile.txt' : No such a file or directory." So I think file permission of the two files are not the same (or I have some misunderstanding?) When I output os.stat('file0.py') and os.stat('logfile.txt') I see the same st_mode. Why ? Shouldn't st_modes be distinct because file permissions are distinct??

16th Mar 2024, 7:25 AM
Ali_combination
Ali_combination - avatar
3 Antworten
+ 1
If you are relying on the sololearn playground, be aware that some background processes create temporary files in the virtual environment, where your code is run. You are trying to delete something that is not there, or not there permanently. When you run the code again, the playground resets to its default state. It would be more productive if you posted a link to the code you are experimenting with.
16th Mar 2024, 12:20 PM
Tibor Santa
Tibor Santa - avatar
0
Unlink does not consider file permissions. More likely, the path to the file is incorrect (compared to the current directory, or the location of your python script). Try with specifying the full absolute path to see if it makes a difference. You can also check this topic about handling errors when file is missing. https://stackoverflow.com/questions/10840533/most-pythonic-way-to-delete-a-file-which-may-not-exist
16th Mar 2024, 10:35 AM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa the following code results in the same output: import os, pathlib path = pathlib.Path.cwd().joinpath("logfile.txt") os.unlink(path) If the "path" didn't exist, the FileNotFound exception would raise. I ran the code with "path = pathlib.Path.cwd().joinpath("logfile0.txt")" and a different exception raised
16th Mar 2024, 11:57 AM
Ali_combination
Ali_combination - avatar