开发者

File Locking in Python? [duplicate]

开发者 https://www.devze.com 2023-02-02 02:50 出处:网络
This question already has answers here: Locking a file in Python (15 answers) Closed 9 years ago. I want to lock the file whenever I perform read/write operation o开发者_JAVA技巧n that fi
This question already has answers here: Locking a file in Python (15 answers) Closed 9 years ago.

I want to lock the file whenever I perform read/write operation o开发者_JAVA技巧n that file. No other program or function can access file until I do not release the file. How do you do this in Python?


You can try this:

http://docs.python.org/library/fcntl.html#fcntl.flock

or this:

http://docs.python.org/library/fcntl.html#fcntl.lockf

To get file descriptor from a file-like object, you just need to call fileno method.


Google up zc.lockfile or portalocker.py. Both libraries can lock a file in a portable manner (windows and posix systems). I usually use zc.lockfile.

0

精彩评论

暂无评论...
验证码 换一张
取 消