I am opening a file in python using with command as given below. Then I am copying the object to w.
import os
os.chdir(r"C:\Users\")
with open(r"abc.040", 'r+') as k:
w = k
for a in w:
print(a)
But when I try to iterate w object through for loop, I am getting below error.
Traceback (most recent call last):
File "C:/Users/w.py", line 8, in <module>
for a in w:
ValueError: I/O operation on closed file.
How to copy a file instance
=and no copies. So you are still using the same object just under a new name.withblock, the file gets automatically closed when you exit the block.k(andw, they are just two names for the same file object) is closed when you reach theforloop.w = kis not a copy.