在Python中删除多个指定的文件夹(+内容)

时间:2016-06-08 15:36:15

标签: python delete-file directory multiple-files shutil

我试图学习Phython所以我还不知道很多这种编码语言;)

我试图用python脚本删除一些预先指定的文件夹(包括它的内容),我知道如何删除这样的ONE文件夹:

def purgeOld():
    import os,xbmc,shutil
    xvbmc = os.listdir(xbmc.translatePath(os.path.join('special://home/addons/')))
    addonfolder = xbmc.translatePath(os.path.join('special://home/addons/'))
    for item in xvbmc:
        if ('plugin.video.Blaaat0') in item:
            print str(xvbmc)+str(item)
            try:
                shutil.rmtree(addonfolder+item, ignore_errors=True)
            except:
                pass
        else:
            pass
    return

现在我尝试使用多个文件夹执行此操作,这是我所拥有的并且几乎正常工作,我确实删除了指定文件夹的内容但不删除文件夹本身,我该如何更改此内容或者,我应该完全不同吗? 我从这里复制/粘贴了一些谷歌搜索结果并将其转换为“Frankencode”' ; - p

class cacheEntry:
    def __init__(self, namei, pathi):
        self.name = namei
        self.path = pathi

def setupXvbmcEntries():
    entries = 3 #make sure this refelcts the amount of entries you have
    dialogName = ["Blaaat1", "Blaaat2", "Blaaat3"]
    pathName = ["special://home/addons/repository.Blaaat1","special://home/addons/repository.Blaaat2",
                "special://home/addons/repository.Blaaat3"]

    XvbmcEntries = []

    for x in range(entries):
        XvbmcEntries.append(cacheEntry(dialogName[x],pathName[x]))

    return XvbmcEntries

def purgeOld():
    import os,xbmc,shutil
#    xvbmc = os.listdir(xbmc.translatePath(os.path.join('special://home/addons/')))
#    addonfolder = xbmc.translatePath(os.path.join('special://home/addons/'))
#    for item in xvbmc:
#        if ('plugin.video.Blaaat0') in item:
#            print str(xvbmc)+str(item)
#            try:
#                shutil.rmtree(addonfolder+item, ignore_errors=True)
#            except:
#                pass
#        else:
#            pass

    XvbmcEntries = setupXvbmcEntries()

    for entry in XvbmcEntries:
        xvbmcaddons = xbmc.translatePath(entry.path)
        if os.path.exists(xvbmcaddons)==True:    
            for root, dirs, files in os.walk(xvbmcaddons):
                file_count = 0
                file_count += len(files)
                if file_count > 0:

                        for f in files:
                            os.unlink(os.path.join(root, f))
                        for d in dirs:
                            shutil.rmtree(os.path.join(root, d))

                else:
                    pass

    return

希望我非常接近?或者我应该以更清洁/更好的方式做到这一点?亲切的问候,蒂姆。

2 个答案:

答案 0 :(得分:0)

第一次尝试/测试,将其更改为:

def purgeOld():

    XvbmcEntries = setupXvbmcEntries()

    for entry in XvbmcEntries:
        xvbmcaddons = xbmc.translatePath(entry.path)
        if os.path.exists(xvbmcaddons)==True:    
            for root, dirs, files in os.walk(xvbmcaddons):
                file_count = 0
                file_count += len(files)
                if file_count > 0:

                    for f in files:
                        try:
                            os.unlink(os.path.join(root, f))
                        except:
                            pass
                    for d in dirs:
                        try:
                            shutil.rmtree(os.path.join(root, d), ignore_errors=True)
                        except:
                            pass

                    try:
                        shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/repository.Blaaat1')), ignore_errors=True)
                        shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/repository.Blaaat2')), ignore_errors=True)
                        shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/repository.Blaaat3')), ignore_errors=True)
                        dialog.ok("TEST-PURGE", 'we found some orphaned dependencies...','', 'NOTE: a REBOOT is highly recommended!')
                        xbmc.executebuiltin("UpdateLocalAddons")
                    except:             
                        pass

            else:
                #dialog.ok("Purge test dialog2", "Crap cleaner all done...")
                pass
        else:
            #dialog.ok("Purge test dialog3", "Crap cleaner all done...")
            pass

    dialog.ok("Purge dialog DONE!", "everything is as clean as a whistle...")
#   return

这确实删除了所有文件夹,但现在我必须指定“删除什么”两次,这看起来有点多余? (条目在之前的setupXvbmcEntries中指定,现在再次/在try下使用:等等),我认为这可以做得更漂亮,再次以某种方式调用'xvbmcaddons'?

答案 1 :(得分:0)

第二次尝试/测试, 并且仍然需要双重删除(第一次运行以某种方式留下空目录)...

def purgeOLD():

    XvbmcEntries = setupXvbmcEntries()

    for entry in XvbmcEntries:
        xvbmcaddons = xbmc.translatePath(entry.path)
        if os.path.exists(xvbmcaddons)==True:    
            for root, dirs, files in os.walk(xvbmcaddons):
                file_count = 0
                file_count += len(files)
                if file_count > 0:

                    for f in files:
                        try:
                            os.unlink(os.path.join(root, f))
                        except OSError:
                            os.remove(os.path.join(root, f))
                    for d in dirs:
                        try:
                            shutil.rmtree(os.path.join(root, d), ignore_errors=True)
                        except:
                            pass

                else:
                    shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/','plugin.video.Blaaat1')), ignore_errors=True)
                    shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/','repository.Blaaaaat2')), ignore_errors=True)
                    shutil.rmtree(xbmc.translatePath(os.path.join('special://home/addons/','repository.Blaaaaat3')), ignore_errors=True)
                    dialog.ok("TEST-PURGE", 'we found some orphaned dependencies...','', 'NOTE: a REBOOT is highly recommended!')
                    xbmc.executebuiltin("UpdateLocalAddons")
                    pass

        else:
          # dialog.ok("Purge test dialog", "Crap cleaner all done...")
            pass

    dialog.ok("-= ALL DONE =- ", 'your system seems in good condition','', '(everything is as clean as a whistle)')