【问题标题】:Folder Missing But No confirmation文件夹丢失但没有确认
【发布时间】:2015-05-16 23:49:30
【问题描述】:

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

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

【问题讨论】:

    标签: python xbmc shutil os.path kodi


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我不知道这是如何跨平台的,但对我来说,你的问题是 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
      

      【讨论】:

      • 不要使用字符串进行这些测试。使用errno 模块。
      【解决方案3】:

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

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

      正确显示通知。

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-27
        • 2012-12-20
        相关资源
        最近更新 更多