Context managers: with (file) experiments
examples/advanced/with_file_write.py
with open('out.txt', 'w') as h: h.write("hello\n") h = open('out.txt') print(h.read())
f = open('out.txt', 'w') f.write("hello\n") f.close() # for line in open("myfile.txt"): # print line, # the file is closed only when script ends