When you open a file, it\'s stored in an open file object which gives you access to various methods on it such as reading or writing.
>>> f = open(\"fil
One use is using the name to reopen the file:
open(f.name).read()
I use the name attribute when changing a file content using a NamedTemporaryFile to write the updated content to then replace the original file with shutil.move:
with open("foo.txt") as f, NamedTemporaryFile("w", dir=".", delete=False) as temp:
for line in f:
if stuff:
temp.write("stuff")
shutil.move(temp.name, "foo.txt")
Also as commented you can use the f.closed
to see if the file is really closed.