Python Forum

Full Version: Delete file with read-only permission, but write permission to parent folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
You need to do is run your Python script with elevated privileges.
cmd as Administrator in Windows 8 or 10.
cubei Wrote:Python 3.3.2
You should update.
Python 3.6/3.7 and pip installation under Windows
(Aug-27-2018, 03:24 PM)snippsat Wrote: [ -> ]
cubei Wrote:Python 3.3.2
You should update.
This was a on corporate computer. Here on my private Notebook I can reproduce it with Python 3.6.3.

(Aug-27-2018, 03:24 PM)snippsat Wrote: [ -> ]You need to do is run your Python script with elevated privileges.
cmd as Administrator in Windows 8 or 10.
Why do I need admin rights to delete a file I created myself. I don't have admin rights and I also don't need them because I can simply delete the file in the windows explorer without elevated privileges. Like mentioned in my first post, write access to the parent folder should be enough.

But I noticed, that in the command prompt I also can't delete the file with the "del" command. So it's not a python bug like I suspected. It seems to be normal that read-only files can't be deleted (makes no sense from my point of view) and I just got confused because the Windows Explorer let's me do it. Huh

So before I copy the file, I should check first if the target file already exist and then remove the read-only flag:
import os
import stat
os.chmod(filePath, stat.S_IWRITE)
(Aug-27-2018, 06:30 PM)cubei Wrote: [ -> ]So before I copy the file, I should check first if the target file already exist and then remove the read-only flag:
Yes have to this for read only.
So it i test and catch the error.
import os
import stat

file_name = 'foo.txt'
try:
    os.remove(file_name)
except PermissionError:
    print('PermissionError do change')
    os.chmod(file_name, stat.S_IWRITE)
    os.remove(file_name)
os.chmod() is maybe the best option as is in stander library and work.
Did test one from win32api and it also work.
import os
import win32con, win32api

file_name = 'foo.txt'
# Force deletion of a file set it to normal
win32api.SetFileAttributes(file_name, win32con.FILE_ATTRIBUTE_NORMAL)
os.remove(file_name)
In case someone is interested in my solution for copying a file and overwriting it in case it is already available:

import os
import shutil
import filecmp
import stat

def copyFile(sourceFile, destinationFile):
  if not os.path.isfile(destinationFile) or not filecmp.cmp(sourceFile, destinationFile):
    if os.path.isfile(destinationFile):
      os.chmod(destinationFile, stat.S_IWRITE) # Make sure the file is not read-only
    shutil.copyfile(sourceFile, destinationFile)