diff --git a/src/watchdog/observers/read_directory_changes.py b/src/watchdog/observers/read_directory_changes.py index 3b816353..91738be8 100644 --- a/src/watchdog/observers/read_directory_changes.py +++ b/src/watchdog/observers/read_directory_changes.py @@ -46,7 +46,10 @@ def __init__( self._whandle: HANDLE | None = None def on_thread_start(self) -> None: - self._whandle = get_directory_handle(self.watch.path) + watch_path = self.watch.path + if os.path.isfile(watch_path): + watch_path = os.path.dirname(watch_path) + self._whandle = get_directory_handle(watch_path) if platform.python_implementation() == "PyPy": @@ -71,7 +74,12 @@ def queue_events(self, timeout: float) -> None: with self._lock: last_renamed_src_path = "" for winapi_event in winapi_events: - src_path = os.path.join(self.watch.path, winapi_event.src_path) + if os.path.isfile(self.watch.path): + if os.path.basename(self.watch.path) != winapi_event.src_path: + continue + src_path = self.watch.path + else: + src_path = os.path.join(self.watch.path, winapi_event.src_path) if winapi_event.is_renamed_old: last_renamed_src_path = src_path