os.rmdir & os.remove 与 Python 中的排除部分工作
Posted
技术标签:
【中文标题】os.rmdir & os.remove 与 Python 中的排除部分工作【英文标题】:os.rmdir & os.remove with excludes in Python partially works 【发布时间】:2018-05-31 18:50:33 【问题描述】:我的 Python 脚本与 os.rmdir (dirs[:]) & os.remove (files[:]) 与特定排除部分工作,但是 它确实删除了一点点...
例如 /media/ 文件夹 (dir) 也被删除,但我确实尝试排除它,这是我的示例代码:
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\ 文件夹:
if keep_xvbmc:
dir_exclude = ('addon_data','media',)+dir_exclude
我认为问题出在这部分???
for file_name in files:
这个可能删除太多,所以文件夹 /media/ 也完全为空,这就是它被删除的原因? Common.REMOVE_EMPTY_FOLDERS() 仅删除完全 empty 剩余部分。
我应该以某种方式将 dir_exclude 集成到这里吗? (在/下 for file_name in files:),我正在尝试,但我似乎无法弄清楚 :'(
我也试过了,现在 /media/ 文件夹没有弄乱,但是现在太多文件夹仍然存在:
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))
L-- 如你所见,我在下一行添加了这一行:
if not any(root.find(f) for f in dir_exclude):
当我将 'media' 添加到 sub_dir_exclude 时,所有带有 'media' 的文件夹都会被跳过,这也不是我想要的,只有 xbmcPath 中的 'media' 文件夹必须保留(从删除中跳过)。
xbmcPath 是我的根/开始文件夹,它“有点像”:xbmc.translatePath('special://home/') ,从这一点开始,我执行 os.walk(xbmcPath,topdown=True)。
【问题讨论】:
那里有很多无用的代码。请查看[SO]: How to create a Minimal, Complete, and Verifiable example (mcve)。 【参考方案1】:没关系,这成功了:
if len(bestanden) > 1:
vh = dialog.select('Selecteer bestand om te downloaden', bestanden)
if vh == -1:
return
else:
vh = 0
【讨论】:
以上是关于os.rmdir & os.remove 与 Python 中的排除部分工作的主要内容,如果未能解决你的问题,请参考以下文章