Deletion of Read only files on a windows machine running a python script

眉间皱痕 提交于 2019-12-08 03:12:06

问题


I have some files on a windows machine(directory d:/test/temp/). For some of the files i have read only permission. For the deletion of the files/folder in the above directory I use a python scripts which recursively iterates over the directory and deletes every file in it.

Following is the snippet of code used for the deletion:

for entry in  listdir(dest_folder):  
    if isfile(join(dest_folder,entry)) and  basename(filename) != entry:  
       remove(join(dest_folder,entry))

I use a user named: tectt which has all permissions to delete the file.

When I logged into the windows machine with that user I was able to delete the read only files manually. But when I try to delete the read only files through python scripts, I was unable to delete the files.

An error was thrown saying: [Error 5] Access is denied

I'm new to python scripting. Can some please help me out:
1. To delete these read only files with scripts?
2. Am i missing any things, if so, what would have been them?

Regards,
Vijay


回答1:


The solution is stated here. Just try to use the search function.

import os, stat
os.chmod(path, stat.S_IWRITE)
os.unlink(path)

How to remove read-only attrib directory with Python in Windows?



来源:https://stackoverflow.com/questions/26380862/deletion-of-read-only-files-on-a-windows-machine-running-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!