我想通过 try catch 结构来具体实现这一点。
这related question建议我可以这样做:
try:
open(fileName, 'wb+')
except:
print("File already opened!")
raise
但是,这对我不起作用。我可以毫无问题地多次打开同一个文件:
fileObj1 = open(fileName, 'wb+')
fileObj2 = open(fileName, 'wb+')
是因为我有 Python 3.5 吗?或者因为我正在使用 Raspbian ?
感谢您的帮助!
最佳答案
您应该打开同一个文件,但将它们分配给不同的变量,如下所示:
file_obj = open(filename, "wb+")
if not file_obj.closed:
print("File is already opened")
.closed
只检查文件是否被同一个 Python 进程打开。
关于python - 如何检查文件是否已经打开(在同一进程中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37515574/