删除旧SVN文件的Python脚本缺乏权限

发布于 2024-09-05 01:05:33 字数 1511 浏览 6 评论 0原文

我正在尝试从目录树中删除旧的 SVN 文件。 shutil.rmtreeos.unlink 引发 WindowsError,因为脚本没有删除它们的权限。我该如何解决这个问题?

这是脚本:

# Delete all files of a certain type from a direcotry

import os
import shutil

dir = "c:\\"

verbosity = 0;

def printCleanMsg(dir_path):
    if verbosity:
        print "Cleaning %s\n" % dir_path

def cleandir(dir_path):
    printCleanMsg(dir_path)
    toDelete = []
    dirwalk = os.walk(dir_path)
    for root, dirs, files in dirwalk:
        printCleanMsg(root)
        toDelete.extend([root + os.sep + dir for dir in dirs if '.svn' == dir])
        toDelete.extend([root + os.sep + file for file in files if 'svn' in file])

    print "Items to be deleted:"
    for candidate in toDelete:
        print candidate
    print "Delete all %d items? [y|n]" % len(toDelete)

    choice = raw_input()

    if choice == 'y':
        deleted = 0
        for filedir in toDelete:
            if os.path.exists(filedir): # could have been deleted already by rmtree
                try:
                    if os.path.isdir(filedir):
                        shutil.rmtree(filedir)
                    else:
                        os.unlink(filedir)
                    deleted += 1
                except WindowsError:
                    print "WindowsError: Couldn't delete '%s'" % filedir

    print "\nDeleted %d/%d files." % (deleted, len(toDelete))
    exit()

if __name__ == "__main__":
    cleandir(dir)

没有一个文件能够被删除。我做错了什么?

I'm trying to delete old SVN files from directory tree. shutil.rmtree and os.unlink raise WindowsErrors, because the script doesn't have permissions to delete them. How can I get around that?

Here is the script:

# Delete all files of a certain type from a direcotry

import os
import shutil

dir = "c:\\"

verbosity = 0;

def printCleanMsg(dir_path):
    if verbosity:
        print "Cleaning %s\n" % dir_path

def cleandir(dir_path):
    printCleanMsg(dir_path)
    toDelete = []
    dirwalk = os.walk(dir_path)
    for root, dirs, files in dirwalk:
        printCleanMsg(root)
        toDelete.extend([root + os.sep + dir for dir in dirs if '.svn' == dir])
        toDelete.extend([root + os.sep + file for file in files if 'svn' in file])

    print "Items to be deleted:"
    for candidate in toDelete:
        print candidate
    print "Delete all %d items? [y|n]" % len(toDelete)

    choice = raw_input()

    if choice == 'y':
        deleted = 0
        for filedir in toDelete:
            if os.path.exists(filedir): # could have been deleted already by rmtree
                try:
                    if os.path.isdir(filedir):
                        shutil.rmtree(filedir)
                    else:
                        os.unlink(filedir)
                    deleted += 1
                except WindowsError:
                    print "WindowsError: Couldn't delete '%s'" % filedir

    print "\nDeleted %d/%d files." % (deleted, len(toDelete))
    exit()

if __name__ == "__main__":
    cleandir(dir)

Not a single file is able to be deleted. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-09-12 01:05:34

为了递归地删除所有 .svn,我使用这个脚本。也许它会对某人有所帮助。

import os, shutil, stat

def del_evenReadonly(action, name, exc):
    os.chmod(name, stat.S_IWRITE)
    os.remove(name)

for root, subFolders, files in os.walk(os.getcwd()):
    if '.svn' in subFolders:
      shutil.rmtree(root+'\.svn',onerror=del_evenReadonly)

To remove recursively all .svn I use this script. May be it will help someone.

import os, shutil, stat

def del_evenReadonly(action, name, exc):
    os.chmod(name, stat.S_IWRITE)
    os.remove(name)

for root, subFolders, files in os.walk(os.getcwd()):
    if '.svn' in subFolders:
      shutil.rmtree(root+'\.svn',onerror=del_evenReadonly)
木格 2024-09-12 01:05:34

Subversion 通常会使所有 .svn 目录(以及其中的所有内容)受到写保护。可能您必须先删除写保护,然后才能删除文件。

我不太确定如何在 Windows 上做到最好,但您应该能够使用 os.chmod() 带有 stat.S_IWRITE 标志。也许您必须遍历 .svn 目录中的所有文件并使它们全部可单独写入。

Subversion usually makes all the .svn directories (and everything in them) write protected. Probably you have to remove the write protection before you can remove the files.

I'm not really sure how to do this best with Windows, but you should be able to use os.chmod() with the stat.S_IWRITE flag. Probably you have to iterate through all the files in the .svn directories and make them all writable individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文