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