Simple folder/file lock handler I have two python scripts (or one bash script + one python script) which are working independently from another, one script puts data into a folder, the other one reads from it.
Since I don't want to accidentally read from the folder at the same time it's updated, I created this simple folder lock using a dummy file.
In the bash script a similar thing is done, by checking whether that lock.file exists, and if not create it, put the current PID into it, and after it's done delete it.
import os
import sys
import time
import logging

LOGGER = logging.getLogger()

class SimpleFolderLock():
    def __init__(self, folder_path, time_out=5):
        self._folder_to_lock = folder_path
        self._lock_file_path = os.path.join(self._folder_to_lock, &quot;folder.lock&quot;)
        self._lock_time_out = time_out
        
    def __enter__(self):
        start_time = time.time()
        while os.path.exists(self._lock_file_path):
            LOGGER.debug(f&quot;Waiting for {self._lock_file_path} to clear&quot;)
            time.sleep(5)
            if (time.time() - start_time) &gt; self._lock_time_out:
                LOGGER.error(
                    f&quot;Time-out for waiting for {self._lock_file_path} to clear&quot;)
                raise TimeoutError(f&quot;Can't lock {self._folder_to_lock} after {self._lock_time_out} seconds&quot;)
    
        LOGGER.debug(f&quot;Locking with {self._lock_file_path}&quot;)
        with open(self._lock_file_path, &quot;w&quot;) as fn:
            fn.write(str(os.getpid()))
            
        return self._lock_file_path
    
    def __exit__(self, type, value, traceback):
        try:
            LOGGER.debug(f&quot;Clearing {self._lock_file_path}&quot;)
            os.remove(self._lock_file_path)
        except FileNotFoundError:
            LOGGER.error(f&quot;File {self._lock_file_path} already got deleted&quot;)

And for a simple test:
# Just some dummy logging configuration
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
test_folder = &quot;/tmp/test_dir/&quot;
os.makedirs(test_folder, exist_ok=True)
with SimpleFolderLock(test_folder) as lock:
    lock_path = lock
    LOGGER.info(f&quot;Locked with {lock_path}&quot;)
    assert os.path.exists(lock_path)
    fiddle_around_in_folder(test_folder)

    
assert not os.path.exists(lock_path)
There is a race condition here:
while os.path.exists(self._lock_file_path):
...
with open(self._lock_file_path, &quot;w&quot;) as fn:

The file could have been created by the other process in between. You need to check file existence and create the file in one operation.
Since python 3.3 there is a special open mode x for this use case:

open for exclusive creation, failing if the file already exists

For example:
while (time.time() - start_time) &lt; self._lock_time_out:
    try:
        with open(self._lock_file_path, &quot;x&quot;) as fn:
            fn.write(str(os.getpid()))
    except FileExistsError:
        time.sleep(5)