diff --git a/src/filelock/_unix.py b/src/filelock/_unix.py index 4ee6033..4ae1fbe 100644 --- a/src/filelock/_unix.py +++ b/src/filelock/_unix.py @@ -4,6 +4,7 @@ import sys from contextlib import suppress from errno import ENOSYS +from pathlib import Path from typing import cast from ._api import BaseFileLock @@ -35,7 +36,9 @@ class UnixFileLock(BaseFileLock): def _acquire(self) -> None: ensure_directory_exists(self.lock_file) - open_flags = os.O_RDWR | os.O_CREAT | os.O_TRUNC + open_flags = os.O_RDWR | os.O_TRUNC + if not Path(self.lock_file).exists(): + open_flags |= os.O_CREAT fd = os.open(self.lock_file, open_flags, self._context.mode) with suppress(PermissionError): # This locked is not owned by this UID os.fchmod(fd, self._context.mode)