Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKerrick Hahn
5,728 PointsCan someone explain 5:38 - 6:02 in the video please? He ends with "The file being open, the file would be closed"...??
When Kenneth starts to explain the importance and functionality behind closing a file, he makes about 3 pretty bizarre analogies that seem more like paradox's. If anyone can explain what he's saying here, that would be much appreciated. Thanks in advance!!!
Kerrick Hahn
5,728 PointsTrack: Python Intermediate track Course (actually a 'workshop'): Python File I/O Video: Writing to Files Time: 5:38 - 6:10
2 Answers
Peter Vann
36,427 PointsWhat he is referring to is that unlike some other programming languages, python does its own 'garbage-collection', which is a slangy way of saying cleaning-up things like freeing-up memory and closing files, etc.
In this case, he is saying that, since Python does its own garbage collection, operations such a freeing-up memory used by - and closing files opened by - the function will automatically happen at the end of the function call.
"The file being open, the file would be closed"...?!? (automatically - at the end of the function execution)
He then goes on to say that, regardless, he prefers to explicitly close any open files with code in his functions, primarily for his own assurance and/but I agree that it does add clarity to the code.
"You open the file, write to it, then close it."
I hope that helps. Happy coding!
By the way, the fact that Python does do its own garbage-collection is one of the reasons it is easy, efficient, fun, slick, and so popular. That, and the fact that Python, for the most part, is far less verbose than other languages, such as Java.
Kerrick Hahn
5,728 PointsThank you Peter Vann !!! I appreciate you taking the time to answer!
Peter Vann
36,427 PointsYou are very welcome!
Have fun learning Python - I LOVE IT!
Peter Vann
36,427 PointsPeter Vann
36,427 PointsTo what course/video/challenge are you referring?