Now we've seen the basic ingredients, it's time to abandon them and use the much safer with block (and the Path class and the writelines function)
home=Path.home()# works on windows/linux/mac/...
lines=("that quick brown fox","jumps over")withPath(home/'myfile.txt').open('a')asfile:file.writelines(lines)file.write("1 + 2 "+3+" = "+1+2+3)# terrible TypeError here
Adding fallible code in a with block ensures that the file gets flushed and closed, exception or not.
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & practise Sahaja Yoga meditation. Do follow me on GitHub https://github.com/Aatmaj-Zephyr
Thanks for that. I wish I had more time for this course so I could clearly explain everything in depth. Things like exception handling, more file methods and binary files afe left unfinished. But unfortunately I will require to finish up the intermediate part of the course this week. Hope one day I will write about this all. Thanks once again for the insightful piece of code.
You're welcome. I appreciate the effort you put into this, but I'm not sure if I agree with your telling aspiring programmers to adopt a way that has proven to cause expensive problems: improper cleanup of opened resources (files, sockets, database connections, ...). This open/close paradigm relies entirely on developer discipline. It was common in the 1990s, but since the advent of functional programming, this can be handled by our languages and libraries.
In this specific case, consider what is easier to learn
'you always have to close your file (and I won't tell you how to make sure it happens)`, or
'you open a file by using a with x.open() as f: block (and safety is built-in for you)?
This may sound harsh, but I'm appealing to your responsibility as a teacher. And given the fact that I have to spend time unteaching my young colleagues' bad habits, after fixing their bugs, I feel entitled to bring this message.
You may also find this message, amongst others, repeated in this terrific talk by one of the most experienced C++ teachers: Stop Teaching C.
But again: it's really great that you take the time to write all of this down! Keep up the good work!
I'm a Computer Science enthusiast with a keen interest in Maths. I like to tinker with things around me & practise Sahaja Yoga meditation. Do follow me on GitHub https://github.com/Aatmaj-Zephyr
Now we've seen the basic ingredients, it's time to abandon them and use the much safer
with
block (and thePath
class and thewritelines
function)Adding fallible code in a
with
block ensures that the file gets flushed and closed, exception or not.Thanks for that. I wish I had more time for this course so I could clearly explain everything in depth. Things like exception handling, more file methods and binary files afe left unfinished. But unfortunately I will require to finish up the intermediate part of the course this week. Hope one day I will write about this all. Thanks once again for the insightful piece of code.
You're welcome. I appreciate the effort you put into this, but I'm not sure if I agree with your telling aspiring programmers to adopt a way that has proven to cause expensive problems: improper cleanup of opened resources (files, sockets, database connections, ...). This open/close paradigm relies entirely on developer discipline. It was common in the 1990s, but since the advent of functional programming, this can be handled by our languages and libraries.
In this specific case, consider what is easier to learn
with x.open() as f:
block (and safety is built-in for you)?This may sound harsh, but I'm appealing to your responsibility as a teacher. And given the fact that I have to spend time unteaching my young colleagues' bad habits, after fixing their bugs, I feel entitled to bring this message.
You may also find this message, amongst others, repeated in this terrific talk by one of the most experienced C++ teachers: Stop Teaching C.
But again: it's really great that you take the time to write all of this down! Keep up the good work!
Thanks for the advice๐