0

I have tried many methods, some from How to remove read-only attrib directory with Python in Windows? but none work. Still getting the WinError Access Denied message. Simply need to access a file location, remove it, then put a different file in that folder.

3Dprog
  • 11
  • Please share the method you were most confident should work and show what you tried. – Grismar Oct 21 '22 at 02:11
  • If the user account that your script is running as doesn't have access to some location, there's really no way to get around that (it'd be a serious security problem if there was), so you'd want to make sure it is just a file permissions issue, and that the account your script is running as actually has the rights to make the changes you're looking to make. – Grismar Oct 21 '22 at 03:04
  • @Grismar I tried the methods on the link, but thought this one would work import os, shutil, stat path = fldr + "/file.txt" def on_rm_error(func, path, exc_info): # path contains the path of the file that couldn't be removed # let's just assume that it's read-only and unlink it. os.chmod(path, stat.S_IWRITE ) os.unlink(path) shutil.rmtree(temp_fldr_at, onerror = on_rm_error ) – 3Dprog Oct 21 '22 at 16:15

1 Answers1

0

Easiest way to do this is using OS library and execute commands in shell, but in big projects, especially commercial ones, It's going to be a security bug =) to use it, just import the OS library and use it like os.system('command').

This function let you run any commands in most Operating Systems In your case, the command for changing the attribute of any file in windows would be "attrib -\+switch"

so you can run it like this:

import os
file_address = ""
os.system(f'attrib -r "{file_address}"')
DrunkLeen
  • 1
  • 3