And I'd like to specifically achieve that with the try catch construct.
This related question suggests that I can do:
try:
open(fileName, 'wb+')
except:
print("File already opened!")
raise
However, it doesn't work me. I can open the same file multiple times without any problem:
fileObj1 = open(fileName, 'wb+')
fileObj2 = open(fileName, 'wb+')
Is it because I have Python 3.5? Or because I'm using Raspbian?
Thanks for the help!
You open the same file but assign them to different variables. What you should do is:
fileobj=open(filename,"wb+")
if not fileobj.closed:
print("file is already opened")`
I'm writing with my phone so the styling may not be good but you will get the point. By the way the .closed
only checks if the file has been opened by the same python process.