4 ms·
Even better when handling files is to use the with statement. with open(filename, 'r') as myfile: data = myfile.read() # do something with
by RyanMcGreal 14y ago
Even better when handling files is to use the with statement.
with open(filename, 'r') as myfile:
data = myfile.read()
# do something with data
The nice thing about with is that it doubles as a try...except block and automatically closes the file for you.
- ColinWright 14y agoYes, but when you're just three days into learning it's good to do things the direct way using fewer language facilities, then grow into the more powerful constructs later when you can better appreciate their value.