Python:检查文件被锁定 [英] Python : Check file is locked

查看:519
本文介绍了Python:检查文件被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是知道一个文件是否被另一个进程锁定,即使我没有访问该文件!

所以要更清楚一点,比方说,我正在用'wb'开关(用于写入)使用python的内建open()打开文件。如果用户没有对文件的权限,那么open()会抛出带errno 13(EACCES)的IOError(


  1. b $ b
  2. 文件被另一个进程锁定

如何在这里检测case(2)?

我的目标平台是Windows!

解决方案

您可以使用 os.access 来检查您的访问权限。如果访问权限是好的,则必须是第二种情况。


My goal is to know if a file is locked by another process or not, even if I don't have access to that file!

So to be more clear, let's say I'm opening the file using python's builtin open() with 'wb' switch (for writing). open() will throw IOError with errno 13 (EACCES) if

  1. the user does not have permission to the file or
  2. the file is locked by another process

How can I detect case (2) here ?

my target platform is Windows!

解决方案

You can use os.access for checking your access permission. If access permissions are good, then it has to be the second case.

这篇关于Python:检查文件被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆