好的,大家好,这是我删除指定文件夹的代码,它是跨平台兼容的,专为Kodi设计。我得到了开发人员的帮助,但是有一些代码缺失,代码底部有更多信息。
import xbmcgui
import xbmc
import os
import shutil
TARGETFOLDER = xbmc.translatePath('special://home/userdata/addon_data/01')
yesnowindow = xbmcgui.Dialog().yesno("This Will Delete a folder","Click yes to delete","Click No to exit")
NOOPTION = xbmc.executebuiltin("ActivateWindow(10000,return)")
if yesnowindow:
os.path.exists(TARGETFOLDER)
if os.path.exists(TARGETFOLDER):
shutil.rmtree(TARGETFOLDER), xbmc.executebuiltin("Notification(Folder has been deleted, All done,()"), xbmc.executebuiltin("ActivateWindow(10000,return)")
else:
NOOPTION
如果按下是按钮并且TARGETFOLDER不存在,我希望它执行此代码,我知道它必须与os.path.exists有关
以Lamens术语
如果是os.path.exists(TARGETFOLDER):shutil.rmtree(TARGETFOLDER),如果os.path.exists(TARGETFOLDER)= false那么
xbmc.executebuiltin("Notification(Ok, All done,()")
感谢您提供任何帮助。
答案 0 :(得分:0)
我不知道这是怎样的跨平台,但对我来说,你的问题是尖叫的尝试/除外。也许你可以根据自己的需要进行充实:
import shutil
my_folder = 'foobar'
try:
shutil.rmtree(my_folder)
print 'folder deleted'
except OSError, e:
the_error = str(e)
if '[Errno 20]' in the_error:
print my_folder, 'is not a directory!'
elif '[Errno 2]' in the_error:
print my_folder, 'did not exist!'
else:
print the_error
答案 1 :(得分:0)
基于您的示例代码以及我从xbmcgui文档中了解的内容:
import xbmcgui
import xbmc
import os
import shutil
TARGETFOLDER = xbmc.translatePath(
'special://home/userdata/addon_data/01'
)
YESNOWINDOW = xbmcgui.Dialog().yesno(
"This Will Delete a folder",
"Click yes to delete",
"Click No to exit")
if YESNOWINDOW:
_MSG = None
if os.path.exists(TARGETFOLDER):
try:
shutil.rmtree(TARGETFOLDER, ignore_errors=False)
xbmc.executebuiltin("Notification(Folder has been deleted, All done,()")
xbmc.executebuiltin("ActivateWindow(10000,return)")
except OSError as rmerr:
_MSG = ("Error with delete dir: {}".format(rmerr))
except Exception as err:
_MSG = ("Error with XBMC call: {}".format(err))
else:
_MSG = ("Folder {} does not appear to be a directory"
.format(TARGETFOLDER))
if _MSG:
xbmc.executebuiltin("Notification({},()".format(_MSG)) # ***
xbmc.executebuiltin("ActivateWindow(10000,return)")
试试这个并报告什么是borked。我没有一个文本框,我可以让xbmc库来验证这一点。
答案 2 :(得分:0)
谢谢,是的,在{}
之后,第27行遗失了一件事_MSG = ("Folder {} does not appear to be a directory"
正确地显示通知。
我已将代码上传到pastebin http://pastebin.com/BS3VQLbb
对每一行都有评论。如果有人有机会看看我是否正确理解代码,那将是很棒的。
我对代码有几个问题,我可以在这里问他们吗?因为我似乎不允许向它寻求帮助。如果可以,请告诉我。再次感谢,