文件夹丢失但未确认

Folder Missing But No confirmation

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

有关

用拉门斯的话说

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

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

感谢您能给我的任何帮助。

我不知道这有多跨平台,但对我来说你的问题尖叫 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

基于您的示例代码和我从 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 库来验证这一点的文本框。

谢谢,是的,它工作得很好,但在第 27 行 {} 之后缺少一个 ,

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

正确显示通知。

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

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

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