Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file with read-only permission, but write permission to parent folder

Tags:

python

My understanding is that, in order to delete a file, I need write permission to the parent folder (for Windows). I don't need write permission to the file itself.

But os.remove gives me "[WinError 5] Access is denied". I can delete that file via the Windows explorer with my user.

shutil.copy copies a file to a folder without problems, but running the script again gives a "[Errno 13] Permission denied", because the file is read-only and can not be overwritten. It makes no sense that I can create files but not delete those afterwards. Fix is to use shutil.copyfile, because then the destination file has no permissions and it can be overwritten in the next run, but then this won't work if the file already exists with the permissions.

How do I delete a file with only read permissions but write permission to the parent folder?

Python 3.3.2

like image 590
cubei Avatar asked Sep 03 '25 03:09

cubei


1 Answers

read-only attribute can be cleared like this.

import os
import stat
os.chmod(filePath, stat.S_IWRITE)
like image 180
cubei Avatar answered Sep 04 '25 23:09

cubei