9

如何用Python判断一个文件是否被占用?

 3 years ago
source link: https://blog.csdn.net/xufive/article/details/111193984
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何用Python判断一个文件是否被占用?

今天有同学问,用os模块的access()能否判断一个文件是否被占用?直觉上,这是行不通的,因为access()返回的是文件的读写属性。为了确认这一点,我简单测试了一下。

>>> import os
>>> fn = r'D:\temp\csdn\t.py' # 测试用的文件
>>> os.access(fn, os.F_OK) # 文件是否存在
True
>>> os.access(fn, os.R_OK) # 文件是否可读
True
>>> os.access(fn, os.W_OK) # 文件是否可写
True
>>> os.access(fn, os.X_OK) # 文件是否可执行
True
>>> fp = open(fn, 'a+') # 以追加写的方式打开文件
>>> os.access(fn, os.F_OK) # 文件当然还在
True
>>> os.access(fn, os.R_OK) # 文件依然可读
True
>>> os.access(fn, os.W_OK) # 文件依然可写
True
>>> os.access(fn, os.X_OK) # 文件依然执行
True
>>> fp.close()

可见,os.access()返回的是文件读写属性,与文件是否被占用没有半毛钱关系。

后来,群里有同学建议说,不妨用try尝试着open文件,如果成功,表示文件没有被占用,如果抛出异常,则表示文件被占用。果真如此吗?还是用代码验证一下吧。

>>> fp1 = open(fn, 'a+')
>>> fp2 = open(fn, 'a+')
>>> fp1.close()
>>> fp2.close()

结果表明,对同一个文件以写的方式打开多次,系统并没有抛出异常。为什么会这样呢?究其原因,是因为文件被打开和文件被占用是两个完全不同的问题。顺便提醒一下,做上面的测试时,不要使用’w’的方式,否则文件内容会被清空。

那么,究竟应该如何用Python判断一个文件是否被占用呢?这个问题还是要回归到操作系统层面来解决,也就是依赖win32api模块。

>>> import win32file
>>> def is_used(file_name):
	try:
		vHandle = win32file.CreateFile(file_name, win32file.GENERIC_READ, 0, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None)
		return int(vHandle) == win32file.INVALID_HANDLE_VALUE
	except:
		return True
	finally:
		try:
			win32file.CloseHandle(vHandle)
		except:
			pass
		
>>> fn = r'D:\temp\csdn\t.py'
>>> is_used(fn)
False
>>> fp = open(fn, 'a+')
>>> is_used(fn)
True
>>> fp.close()
>>> is_used(fn)
False

简单验证了一下,函数is_used()基本可用。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK