python lock using a file

from filelock import FileLock

with FileLock("myfile.txt.lock"):
    print("Lock acquired.")
    with open("myfile.txt"):
        # work with the file as it is now locked

Here is what the above code is Doing:
1. It acquires a lock on myfile.txt.lock.
2. It opens myfile.txt.
3. It does some work with myfile.txt.
4. It closes myfile.txt.
5. It releases the lock on myfile.txt.lock.