How can I handle FileNotFoundError exceptions for Kivy FileChooserController

kebaranas

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)

John Anderson

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I handle network exceptions in VBA?

How can I handle exceptions and errors in a Java web application?

How can I handle exceptions that Process.run throws

How can I handle exceptions with Spring Data Rest and the PagingAndSortingRepository?

How can I handle exceptions in WebAPI 2. methods?

How can I handle exceptions caused in Elmah itself?

how can i handle value error exceptions many times in python

How do I handle exceptions?

How can I handle exceptions using Multer if I need to use middlewares after uploading a file

How must I handle database and JPA exceptions?

Where and how should I handle multiple exceptions?

How do I handle Sign In Exceptions?

how can I handle invalid data rows using custom exceptions in Java?

I can't succeed to correctly handle exceptions in NestJs

how to handle unchecked exceptions?

How to handle exceptions with builders

How Exceptions are handle in EJB?

How to handle FirebaseAuth exceptions

How to handle exceptions in games

How to handle exceptions in Frege?

How to handle exceptions in Kotlin?

Deno: How to handle exceptions

In a python for loop, how do you handle an iterator that can throw exceptions?

How do I handle exceptions in map() in an Observable in RxJava

How do I handle uncaught exceptions and then delegate handling back to the system?

How do I handle exceptions with Django objects.bulk_create()

How should I handle exceptions when using SwingWorker?

If the catch throws exceptions, how should I handle them?

How should I handle exceptions in custom Hamcrest matchers?

TOP Ranking

HotTag

Archive