Python Forum
Delete file with read-only permission, but write permission to parent folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete file with read-only permission, but write permission to parent folder
#1
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
Reply
#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
Reply
#3
(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)
Reply
#4
(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)
Reply
#5
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 215 Jan-14-2021, 08:58 PM
Last Post: snippsat
  how to change the range of read CSV file every time python file runs greenpine 6 220 Dec-08-2020, 10:11 PM
Last Post: greenpine
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 205 Nov-20-2020, 02:26 PM
Last Post: greenpine
  Writing to file in a specific folder evapa8f 5 277 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  can't read QRcode in large file simoneek 0 260 Sep-16-2020, 08:52 AM
Last Post: simoneek
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 288 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  How do I write a single 8-bit byte to a file? MysticLord 2 387 Sep-03-2020, 12:27 PM
Last Post: MysticLord
  Binary File Read Aussie 6 697 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  Python Cut/Copy paste file from folder to another folder rdDrp 4 554 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  unable to write to log file Mekala 2 391 Aug-12-2020, 11:02 PM
Last Post: Mekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020