1

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?

  • Have you looked at this post yet? [Multiple processes write a same CSV file, how to avoid conflict?](https://stackoverflow.com/a/19830928/15489646) – ObjectJosh Mar 31 '21 at 06:44

0 Answers0