open, close

Manfred Lotz ml_news at posteo.de
Sat Aug 31 08:22:05 EDT 2019


Hi there,
This is a beginner question.

I learned that 

with open("foo.txt") as f:
        lines = f.readlines()

using the with-construct is the recommended way to deal with files
making sure that close() always happens.

However, I also could do:

lines = open("foo.txt").readlines()

I have to admit that I'm not sure if in case something bad happens a
close() is done implicitly as in the first example.


Could I use the latter as a substitute for the with-construct? What are
the recommendations of the experts?


-- 
Thanks, Manfred




More information about the Python-list mailing list