Detect and delete locked file in python

后端 未结 3 1746
感情败类
感情败类 2021-02-10 16:57

I want to detect whether a file is locked, using python on Unix. It\'s OK to delete the file, assuming that it helps detects whether the file was locked.

The file could

3条回答
  •  情书的邮戳
    2021-02-10 17:18

    From the fcntl docs:

    fcntl.lockf(fd, operation[, length[, start[, whence]]])

    If LOCK_NB is used and the lock cannot be acquired, an IOError will be raised and the exception will have an errno attribute set to EACCES or EAGAIN (depending on the operating system; for portability, check for both values).

    This uses the underlying unix flock mechanism, so looks like it should do what you want. Also note there is also os.open, which may be more platform-independent.

提交回复
热议问题