首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python语言中带有excludes的os.rmdir & os.remove部分工作

在Python语言中带有excludes的os.rmdir & os.remove部分工作
EN

Stack Overflow用户
提问于 2018-06-01 02:50:34
回答 1查看 188关注 0票数 0

我的Python脚本带有os.rmdir (目录) & os.remove (文件)和特定的excludes部分工作,但它确实删除了一些到很多...

例如,/media/文件夹(dir)也被删除,但我确实尝试将其排除,以下是我的示例代码:

代码语言:javascript
复制
def rejuvXvbmc():
    yes_pressed=Common.message_yes_no("[COLOR dodgerblue]"+AddonTitle+"[/COLOR] [COLOR red][B]- Reset![/B][/COLOR]",'Wilt u uw XvBMC \'build\' volledig opschonen (wipe) en Kodi Krypton [B]leeg[/B] her-configureren?','[COLOR dimgray]Please confirm that you wish you wipe clean your current configuration and reconfigure Kodi.[/COLOR]')
    if yes_pressed:
        dp.create("[COLOR white]"+AddonTitle+"[/COLOR] [COLOR red][B]- Reset![/B][/COLOR]",'Snelle XvBMC Krypton reset, even geduld...',' ', '[COLOR dimgray](Quick XvBMC Krypton reset, please wait...)[/COLOR]')
        profileDir=xbmcaddon.Addon(id=addon_id).getAddonInfo('path'); profileDir=xbmc.translatePath(profileDir);
        xbmcPath=os.path.join(profileDir,"..",".."); xbmcPath=os.path.abspath(xbmcPath); Common.log("rejuvXvbmc.main_XvBMC: xbmcPath="+xbmcPath);
        #  Directories and sub directories not to remove but to sort through
        dir_exclude = ('addons','Database','packages','userdata')
        #  Directories and sub directories Directories to ignore and leave intact
        sub_dir_exclude = ('metadata.album.universal','metadata.artists.universal',
                           'metadata.common.imdb.com','metadata.common.musicbrainz.org','metadata.common.theaudiodb.com','metadata.common.themoviedb.org',
                           'metadata.themoviedb.org','metadata.tvdb.com',
                           'plugin.program.super.favourites','plugin.program.xvbmcinstaller.nl','repository.xvbmc','resource.language.nl_nl','script.xvbmc.updatertools','service.xbmc.versioncheck','script.module.xbmc.shared',
                           'skin.aeon.nox.spin','script.grab.fanart','service.library.data.provider','resource.images.recordlabels.white','resource.images.studios.coloured','resource.images.studios.white','xbmc.gui','script.skinshortcuts','script.module.simplejson','script.module.unidecode')
        #  Files to ignore and not to be removed
        file_exclude = ('guisettings.xml','kodi.log','Textures13.db')               ### USE at least 2 files in 'file_exclude'; or else troubles with 'for file in dbAddons:' ###
        #  Log what is excluded and what not...
        Common.log("XvBMC.dir_exclude="+str(dir_exclude)); Common.log("XvBMC.sub_dir_exclude="+str(sub_dir_exclude)); Common.log("XvBMC.file_exclude="+str(file_exclude));
        ###
       #file_exclude = ('Addons27.db','guisettings.xml','kodi.log','Textures13.db') ### USE at least 2 files in 'file_exclude'; or else troubles with 'for file in dbAddons:' ###
        dbList   = os.listdir(databasePath)
        dbAddons = []
        for file in dbList:
            if re.findall('Addons(\d+)\.db', file):
               dbAddons.append(file)
        for file in dbAddons:
            dbFile = os.path.join(databasePath, file)# ie. this can be (re-)used for removal... =databasePath+file #
            try:
                file_exclude = (file,)+file_exclude                                 ### USE at least 2 files in 'file_exclude'; or else troubles with 'for file in dbAddons:' ###
                Common.log("XvBMC.file_exclude_dB="+str(file_exclude))
            except:
                Common.log("XvBMC.file_exclude_dB=EXCEPTION")

        dp.update(11,'','***Clean: files+folders...')

        keep_xvbmc = Common.message_yes_no("[COLOR white][B]"+AddonTitle+"[/B][/COLOR]",'Wilt u het XvBMC-NL basis \'framework\' handhaven na reset? Verwijderd alles behalve XvBMC (aanbeveling).','[COLOR dimgray](do you wish to keep XvBMC\'s default framework?)[/COLOR]')
        if keep_xvbmc: 
            dir_exclude     = ('addon_data','media',)+dir_exclude
            sub_dir_exclude = ('inputstream.rtmp','keymaps','service.subtitles.addic7ed','service.subtitles.opensubtitles_by_opensubtitles','service.subtitles.opensubtitlesBeta','service.subtitles.podnapisi','service.subtitles.subscene','script.module.resolveurl','script.module.urlresolver',)+sub_dir_exclude
            file_exclude    = ('advancedsettings.xml','favourites.xml','profiles.xml','RssFeeds.xml','sources.xml',)+file_exclude
            Common.log("XvBMC.dir_exclude="+str(dir_exclude))
            Common.log("XvBMC.sub_dir_exclude="+str(sub_dir_exclude))
            Common.log("XvBMC.file_exclude="+str(file_exclude))
        else:
            dir_exclude     = ('addon_data',)+dir_exclude
            sub_dir_exclude = ('inputstream.rtmp',)+sub_dir_exclude
            file_exclude    = ('advancedsettings.xml','RssFeeds.xml',)+file_exclude
            Common.log("XvBMC.dir_exclude="+str(dir_exclude))
            Common.log("XvBMC.sub_dir_exclude="+str(sub_dir_exclude))
            Common.log("XvBMC.file_exclude="+str(file_exclude))
            Superfavo       = xbmc.translatePath(os.path.join(USERADDONDATA,'plugin.program.super.favourites','Super Favourites'))
            SkinShrtct      = xbmc.translatePath(os.path.join(USERDATA,'addon_data','script.skinshortcuts'))
            try:
                shutil.rmtree(Superfavo)    #;Common.log("rejuvXvbmc.keep_xvbmc: XvBMC vOoDoO -4- " + str(Superfavo))
            except Exception as e: Common.log("rejuvXvbmc.keep_xvbmc: XvBMC-vOoDoO @ " + str(e))
            try:
                shutil.rmtree(SkinShrtct)   #;Common.log("rejuvXvbmc.keep_xvbmc: XvBMC vOoDoO -4- " + str(SkinShrtct))
            except Exception as e: Common.log("rejuvXvbmc.keep_xvbmc: XvBMC-vOoDoO @ " + str(e))
        try:
            for root, dirs, files in os.walk(xbmcPath,topdown=True):
                dirs[:] = [dir for dir in dirs if dir not in sub_dir_exclude]
                files[:] = [file for file in files if file not in file_exclude]
                for file_name in files: 
                    try:
                        dp.update(33,'','***Cleaning files...')
                       #os.remove(os.path.join(root,file_name))
                        Common.log(os.path.join(root,file_name))
                    except Exception as e: Common.log("rejuvXvbmc.file_name: User files partially removed - " + str(e))
                for folder in dirs:
                    if folder not in dir_exclude:
                        try:
                            dp.update(33,'','***Cleaning folders...')
                           #os.rmdir(os.path.join(root,folder))
                            Common.log(os.path.join(root,folder))
                        except Exception as e: Common.log("rejuvXvbmc.folder: User folders partially removed - " + str(e))
            dp.update(66,'','***Crap Cleaning...')
            Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS();
            xbmc.sleep(333)
            Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS();
            xbmc.sleep(666)
        except Exception as e:
            Common.log("rejuvXvbmc: User stuff partially removed - " + str(e))
            Common.message("[COLOR dodgerblue]"+AddonTitle+"[/COLOR] [COLOR red][B]- Error![/B][/COLOR]",'...DAT ging niet helemaal goed, controleer uw log...','[COLOR dimgray](XvBMC user files partially removed, please check log)[/COLOR]')
            sys.exit()
        dp.update(99,'','***Cleaning Crap...')
        Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS(); Common.REMOVE_EMPTY_FOLDERS();
        xbmc.sleep(999)
        dp.close()
        dialog.ok("[COLOR dodgerblue]"+AddonTitle+"[/COLOR] [COLOR lime][B]- Reboot![/B][/COLOR]", 'Kodi zal nu afsluiten',' ','[COLOR dimgray](shutdown Kodi now)[/COLOR]')
       #os._exit(1)
    else: dialog.ok("[COLOR dodgerblue]"+AddonTitle+"[/COLOR] [COLOR red][B]- Cancelled![/B][/COLOR]",'Er is geen schone installatie gedaan...',' ','[COLOR dimgray](interrupted by user)[/COLOR]')

这里我排除了de \media\文件夹:

代码语言:javascript
复制
if keep_xvbmc: 
    dir_exclude     = ('addon_data','media',)+dir_exclude

我想问题出在这部分?

代码语言:javascript
复制
for file_name in files:

这个媒体可能删除了太多,所以文件夹//也是完全空的,这就是它被删除的原因吗?Common.REMOVE_EMPTY_FOLDERS()只删除完全空的。

我是否应该以某种方式将dir_exclude集成到内部?( in /under for file_name in files:),我正在尝试,但似乎弄不明白:'(

我也试过了,现在/media/文件夹没有乱七八糟,但是现在有太多的文件夹存在了:

代码语言:javascript
复制
        for file_name in files:
            if not any(root.find(f) for f in dir_exclude):
                try:
                    dp.update(33,'','***Cleaning files...')
                    os.remove(os.path.join(root,file_name))
                   #Common.log(os.path.join(root,file_name))
                except Exception as e: Common.log("rejuvXvbmc.file_name: User files partially removed - " + str(e))

如您所见,我添加了下面这一行:

代码语言:javascript
复制
if not any(root.find(f) for f in dir_exclude):

当我将‘sub_dir_exclude’添加到介质中时,所有带有'media‘的文件夹都会被跳过,这也不是我想要的,只有xbmcPath中的'media’文件夹必须保留(从删除时跳过)。

xbmcPath是我的根/开始文件夹,它“有点像”:xbmc.translatePath('special://home/')从这一点开始,我做了os.walk(xbmcPath,topdown=True)。

EN

回答 1

Stack Overflow用户

发布于 2018-06-05 06:14:37

不要紧,这就是诀窍:

代码语言:javascript
复制
if len(bestanden) > 1:
    vh = dialog.select('Selecteer bestand om te downloaden', bestanden)
    if vh == -1:
        return
else:
    vh = 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50630943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档