I have a file, and several processes which can operate on it. For simplicity let us assume that each process runs the same script:
with open("file.txt", "r+") as file:
data = file.read()
data = data + "Hello\n"
file.seek(0)
file.write(data)
I assume that such a way of file access allows several processes to modify the same file, what results in improper writing (for instance, three processes could get "HeHello\nHello\nllo" as a result). Is there any way to forbid access to the file for some time and still be able to read and write from there from the current process?