A better way to do this, so to create
the file, or read the contents, and close
the file is to use the with context
manager. The way you do that is, again
use the open function, which works the
same, fruits.txt. Now, you say as myfile,
myfile is just a variable,
and then you have to indent. So what you
have done here is you have created this,
myfile equal to open fruits.txt,
exactly the same. The difference here is
that you will write everything that has
to do with processing that file object,
you will have to write everything under
this line, indented under that line as a
block, that's will make things more
organized. So in this case, what we want
to do to the file is to read it, like
that. Once you are done, then you just
close the indentation, so the next
lines will be unindented, like the print line.
[No audio]
See what's going to happen here.
So that is the expected output of the
print content. Even if you delete this,
not to confuse you, again you get the
same output. So this output is actually
of this line.
[No audio]
And closing the file is
not necessary here because the with
context manager will apply the close
method implicitly. So your file will be
closed once this block ends. So please
use the with context manager
and this is how you do it.
[No audio]