Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNotFoundError when watching a non-existent file for creation #1069

Open
8W9aG opened this issue Sep 17, 2024 · 2 comments
Open

FileNotFoundError when watching a non-existent file for creation #1069

8W9aG opened this issue Sep 17, 2024 · 2 comments

Comments

@8W9aG
Copy link

8W9aG commented Sep 17, 2024

I have the following code that watches a path with the intent to signal when a file is created:

observer = Observer()
observer.schedule(event_handler, path=wait_file, recursive=False)
observer.start()

I receive the following error:

.tox/py310-tests/lib/python3.10/site-packages/xxx/xxx/wait.py:22: in wait_for_file
    observer.start()
.tox/py310-tests/lib/python3.10/site-packages/watchdog/observers/api.py:264: in start
    emitter.start()
.tox/py310-tests/lib/python3.10/site-packages/watchdog/utils/__init__.py:75: in start
    self.on_thread_start()
.tox/py310-tests/lib/python3.10/site-packages/watchdog/observers/inotify.py:119: in on_thread_start
    self._inotify = InotifyBuffer(path, recursive=self.watch.is_recursive, event_mask=event_mask)
.tox/py310-tests/lib/python3.10/site-packages/watchdog/observers/inotify_buffer.py:30: in __init__
    self._inotify = Inotify(path, recursive=recursive, event_mask=event_mask)
.tox/py310-tests/lib/python3.10/site-packages/watchdog/observers/inotify_c.py:165: in __init__
    self._add_watch(path, event_mask)
.tox/py310-tests/lib/python3.10/site-packages/watchdog/observers/inotify_c.py:377: in _add_watch
    Inotify._raise_error()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    @staticmethod
    def _raise_error() -> None:
        """Raises errors for inotify failures."""
        err = ctypes.get_errno()
    
        if err == errno.ENOSPC:
            raise OSError(errno.ENOSPC, "inotify watch limit reached")
    
        if err == errno.EMFILE:
            raise OSError(errno.EMFILE, "inotify instance limit reached")
    
        if err != errno.EACCES:
>           raise OSError(err, os.strerror(err))
E           FileNotFoundError: [Errno 2] No such file or directory

This is odd because it is true that the file does not exist, but my intent here is to wait for its creation. Is there anyway to achieve this intent on linux (works on Mac)?

@BoboTiG
Copy link
Collaborator

BoboTiG commented Sep 17, 2024

You can't watch an inexistent file from start.

I would rather watch the parent folder and filter the expected file actions, as it seems you know the name of the file.

@8W9aG
Copy link
Author

8W9aG commented Sep 17, 2024

You are right, once I changed to watch the directory where the file was housed this worked. However I do feel this is odd, theres nothing that would indicate to me that this can't or shouldn't be done, after all I am interested in consuming file system events including creation, and I think its reasonable to watch a path that doesn't exist for an event such as creation which can only happen when the object starts in a state of non-existence. This confusion is compounded by the idea it works on Mac in this way but Linux behaves differently, when from the consumption of the library these should behave the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants