with open('/content/test1.txt','r',encoding='utf-8')as f:
f.read()
this does not gives an output
f =open('/content/test1.txt','r',encoding='utf-8')
f.read()
***but this works good ***
with open('/content/test1.txt','r',encoding='utf-8')as f:
f.read()
this does not gives an output
f =open('/content/test1.txt','r',encoding='utf-8')
f.read()
***but this works good ***
with open
is using a context manager. It means that within the block that follows, the __enter__
method is called, then at the end of that block the __exit__
method is called. Between those they are equivalent.
If you called read()
on f
within the block, you'd find the same behavior:
with open('/content/test1.txt', 'r', encoding='utf-8') as f:
f.read()
The nice part of the file context manager is that the close
method is called for you.