kebaranas
kebaranas

Reputation: 319

How can I handle FileNotFoundError exceptions for Kivy FileChooserController

I am currently using Kivy's FileChooserController in picking a file. I want to make my own specified action when the FileChooserController receives a file path that is not found in the system (FileNotFoundError). However, when I tried to use "try:" and "except FileNotFoundError:" the program does not execute the actions under my "except FileNotFoundError:". The program was able to identify the exception however it does not respond to my "except FileNotFoundError:". Is there a way to solve this problem?

I tried reading and understanding of Kivy's ExceptionHandler and ExceptionManager. However, I cannot apply it to my problem. And if you have an example on how to use these, may you provide me and explain. Thanks

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

.py code

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 code

<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)

The console shows this message when I try to input invalid file path.

[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)

Upvotes: 0

Views: 328

Answers (1)

John Anderson
John Anderson

Reputation: 39107

That exception is thrown way down inside the FileChooserController inside a generator method that calls the FileSystem method listdir(). I believe you would have to subclass FileChooserIconView and replace some of its code in order to catch that exception. An easier approach would be to avoid throwing that exception in the first place. To do that just modify your __init__ method of the Browse class:

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 = '/'

Upvotes: 1

Related Questions