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
#6
Try using Longpath tool. It helps.
Reply
#7
To delete a file in Windows using Python, ensure you have write permission to the parent folder. The "[WinError 5] Access is denied" error may occur due to file locks or insufficient privileges. Try running the script as an administrator or adjusting file attributes with os.chmod(file, 0o777) before deletion.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,977 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 903 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,041 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,504 Oct-17-2024, 01:15 AM
Last Post: Winfried
  python read PDF Statement and write it into excel mg24 1 959 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  Read TXT file in Pandas and save to Parquet zinho 2 1,239 Sep-15-2024, 06:14 PM
Last Post: zinho
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,771 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Pycharm can't read file Genericgamemaker 5 1,571 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,662 Jul-19-2024, 06:42 PM
Last Post: snippsat
  The INSERT permission was denied on the object Steven5055 3 2,953 Jun-12-2024, 08:13 AM
Last Post: GregoryConley

Forum Jump:

User Panel Messages

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