我正在修改Kodi中的向导,我希望向导删除“addons”目录中包含的所有文件夹,而不删除我的向导。
该文件夹的目录将使用Kodi内置的“special://”功能。我想删除“special:// home / addons”中的所有内容,但名为“plugin.video.spartan0.12.0”的文件夹除外
我知道python需要使用“xbmc.translatePath”函数来识别文件夹路径,但我不知道如何删除文件夹中的所有内容而不删除“plugin.video.spartan0.12.0”
任何帮助都将不胜感激。
这是我目前拥有的
import os
dirPath = "C:\Users\Authorized User\AppData\Roaming\Kodi\addons"
fileList = os.listdir(dirPath)
for fileName in fileList:
os.remove(dirPath+"/"+fileName)
答案 0 :(得分:0)
这可能有些过分(而且有点草率),但它对我有用:
import os
import shutil
#----------------------------------------------------------------------
def remove(path):
"""
Remove the file or directory
"""
if os.path.isdir(path):
try:
shutil.rmtree(path)
except OSError:
print "Unable to remove folder: %s" % path
else:
try:
if os.path.exists(path):
os.remove(path)
except OSError:
print "Unable to remove file: %s" % path
#----------------------------------------------------------------------
def cleanup(dirpath, folder_to_exclude):
for root, dirs, files in os.walk(dirpath, topdown=True):
for file_ in files:
full_path = os.path.join(root, file_)
if folder_to_exclude not in full_path:
print 'removing -> ' + full_path
remove(full_path)
for folder in dirs:
full_path = os.path.join(root, folder)
if folder_to_exclude not in full_path:
remove(full_path)
if __name__ == '__main__':
cleanup(r'c\path\to\addons', 'plugin.video.spartan0.12.0')