[Tutor] Why do you have to close files?

ian douglas ian.douglas at iandouglas.com
Fri Jan 27 00:27:00 CET 2012


On 1/26/12 3:20 PM, amt wrote:
> Exercise 17, extra credit 6 Learn python the hard way: Find out why
> you had to do output.close() in the code.
>
>
> Code:
>
> output.close()
> input.close()
>
>
> I don't get it. If you don't close input and output it works exactly
> the same as if you would close them, so why do you have to do
> output.close() and input.close()?
>
> Also does it matter if you do: input.close() and then output.close()?
> Is there an order to follow?
>
There's no order to follow, and it's really more about cleaning up after 
yourself than being a necessity. If you were writing to real files, your 
operating system would limit how many open files you could have at any 
time, so you want to make sure you close file handles you're no longer 
using.


More information about the Tutor mailing list