1

I'm trying to implement a Python script that can write files exclusively, no other process are allowed to write into it aside the Python script itself. But other processes can read it. Is this possible with Python?

Ark Medes
  • 233
  • 1
  • 2
  • 9

1 Answers1

0

It seems that you want to "lock" the file. I think that the locking behavior is different in different operating systems, but at least it will disallow writing the file.

Usually in Windows Python just opening the file to write locks the file (from changing, deleting, etc.). In linux the behavior can be different.

Have a look at this: Locking a file in Python

Yunnan
  • 23
  • 7
  • I tried your suggested solution but it doesn't work. Check the source here: https://pastebin.com/Y0Y9qTU3 I executed `echo 'something else' > test.txt` while the Python script is still running but looks like "something else" were successfully written in the file even it's locked – Ark Medes Jan 16 '21 at 00:40