如何处理 Kivy FileChooserController 的 FileNotFoundError 异常

How can I handle FileNotFoundError exceptions for Kivy FileChooserController

我目前正在使用 Kivy 的 FileChooserController 选择文件。当 FileChooserController 接收到在系统中找不到的文件路径(FileNotFoundError)时,我想执行自己指定的操作。但是,当我尝试使用 "try:""except FileNotFoundError:" 时,程序不会执行我的 "except FileNotFoundError:" 下的操作。该程序能够识别异常,但它不响应我的“except FileNotFoundError:”。有没有办法解决这个问题?

我尝试阅读和理解 Kivy 的 ExceptionHandler 和 ExceptionManager。但是,我不能将它应用于我的问题。如果你有一个关于如何使用这些的例子,你可以提供给我并解释一下吗?谢谢

https://kivy.org/doc/stable/api-kivy.base.html?highlight=exceptionhandler#kivy.base.ExceptionHandler

.py代码

class Browse(Popup):
    title = StringProperty('BROWSE')
    path = StringProperty('/')
    filters = ListProperty(['*.csv'])
    callback = ObjectProperty()

    def __init__(self, callback, path, *args, **kwargs):
        super().__init__(*args, **kwargs) 
        self.callback = callback
        try: 
            self.path = path
        except FileNotFoundError:
            popup = Message(title='ERROR', 
            message='Path not found. Returning to root folder.')
            popup.open()
            print('opened')
            self.path = '/'

.kv码

<Browse>:
    size_hint: None, None
    size: 474, 474
    BoxLayout:
        orientation: 'vertical'
        FileChooserIconView:
            id: filechooser
            filters: root.filters
            path: root.path
            on_submit: root.select(self.selection)
        GridLayout:
            size_hint: None, None,
            size: root.width - 25, 45
            cols: 4
            rows: 1
            Widget:
            Widget:
            Button:
                text: 'SELECT'
                background_normal: 'assets/theme/positive.png'
                background_down: 'assets/theme/positive_pressed.png'
                on_release: root.select(filechooser.selection)

当我尝试输入无效的文件路径时,控制台显示此消息。

[ERROR ] Unable to open directory

It also shows this message as well indicating that there is a FileNotFoundError.

FileNotFoundError: [Errno 2] No such file or directory: '/234234'

Before the message above I get these messages as well.

Traceback (most recent call last): File "/home/kebaranas/miniconda3/lib/python3.6/site-packages/kivy/uix/filechooser.py", line 828, in _generate_file_entries for index, total, item in self._add_files(path): File "/home/kebaranas/miniconda3/lib/python3.6/site-packages/kivy/uix/filechooser.py", line 849, in _add_files for f in self.file_system.listdir(path): File "/home/kebaranas/miniconda3/lib/python3.6/site-packages/kivy/uix/filechooser.py", line 168, in listdir return listdir(fn)

该异常在调用 FileSystem 方法 listdir() 的生成器方法中的 FileChooserController 内部抛出。我相信您必须 subclass FileChooserIconView 并替换它的一些代码才能捕获该异常。一种更简单的方法是首先避免抛出该异常。为此,只需修改 Browse class:

__init__ 方法
class Browse(Popup):
    title = StringProperty('BROWSE')
    path = StringProperty('/abba')
    filters = ListProperty(['*.csv'])
    callback = ObjectProperty()

    def __init__(self, callback, path, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.callback = callback
        if os.path.exists(path):
            self.path = path
        else:
            # this would throw the exception
            print('path does not exist')
            self.path = '/'