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
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  KivyMD android app - problem with permission polak7gt 0 247 Jan-18-2024, 01:27 PM
Last Post: polak7gt
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 2,784 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,371 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,308 Nov-09-2023, 10:56 AM
Last Post: mg24
  Potential Permission error on Mac OSX Catalina OWOLLC 1 643 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
Question Special Characters read-write Prisonfeed 1 581 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  logging: change log file permission with RotatingFileHandler erg 0 958 Aug-09-2023, 01:24 PM
Last Post: erg

Forum Jump:

User Panel Messages

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