删除运行 python 脚本的 Windows 机器上的只读文件 [英] Deletion of Read only files on a windows machine running a python script

查看:37
本文介绍了删除运行 python 脚本的 Windows 机器上的只读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 机器上有一些文件(目录 d:/test/temp/).对于某些文件,我具有只读权限.为了删除上述目录中的文件/文件夹,我使用了一个 python 脚本,它递归地遍历目录并删除其中的每个文件.

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))

我使用一个名为:tectt 的用户,该用户拥有删除文件的所有权限.

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

当我使用该用户登录 Windows 计算机时,我能够手动删除只读文件.但是当我尝试通过 python 脚本删除只读文件时,我无法删除这些文件.

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.

抛出一个错误说:[错误 5] 访问被拒绝

我是 Python 脚本的新手.有人可以帮我吗:
1.用脚本删除这些只读文件?
2. 我是否遗漏了任何东西,如果有,它们会是什么?

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?

问候,
维杰

推荐答案

解决方案说明 此处.尝试使用搜索功能.

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

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

如何删除只读属性Windows 中的 Python 目录?

这篇关于删除运行 python 脚本的 Windows 机器上的只读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆