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 trial

Python

Kerrick Hahn
Kerrick Hahn
5,728 Points

Can 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!!!

To what course/video/challenge are you referring?

Kerrick Hahn
Kerrick Hahn
5,728 Points

Track: Python Intermediate track Course (actually a 'workshop'): Python File I/O Video: Writing to Files Time: 5:38 - 6:10

Peter Vann

2 Answers

What 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
Kerrick Hahn
5,728 Points

Thank you Peter Vann !!! I appreciate you taking the time to answer!

You are very welcome!

Have fun learning Python - I LOVE IT!