I am slightly embarrassed to say that today I learnt about 'with open' for reading files in python from Zen of Python.
Read From a File
Use the with open syntax to read from files. This will automatically close files for you.
Bad:
Good:
The with statement is better because it will ensure you always close the file, even if an exception is raised.
There's some good reading on styles and preferred 'Pythonic' ways of coding if you follow the code style guide in The Hitchhiker's Guide to Python.
One other good source is the plethora of online courses available now on Coursera or other sites e.g.
No comments:
Post a Comment