0

好的,大家好,这是我删除指定文件夹的代码,它是跨平台兼容的,专为 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

用拉门的话说

如果 os.path.exists(TARGETFOLDER): shutil.rmtree(TARGETFOLDER),如果 os.path.exists(TARGETFOLDER) = false 那么

    xbmc.executebuiltin("Notification(Ok, All done,()")

感谢你给与我的帮助。

4

3 回答 3

0

我不知道这是多么跨平台,但对我来说,你的问题是 try/except 的尖叫。也许您可以充实这一点以满足您的需求:

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
于 2015-05-17T00:40:38.327 回答
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)")

试试这个并报告什么是无聊的。我没有一个文本框,我可以打开 xbmc 库来验证这一点。

于 2015-05-17T01:32:36.067 回答
0

谢谢,是的,它很好用,一件事是 a ,在 {} 之后的第 27 行中缺失

_MSG = ("Folder {} does not appear to be a directory"

正确显示通知。

我已将代码上传到 pastebin http://pastebin.com/BS3VQLbb

每行都有注释。如果有人有机会看看我是否正确理解了代码,那就太好了。

我确实有几个关于代码的问题,我可以在这里问他们吗?看来我不允许寻求帮助。如果可以问,请告诉我。再次感谢,

于 2015-05-17T16:05:13.907 回答