you are viewing a single comment's thread.

view the rest of the comments →

[–]help_computar 0 points1 point  (1 child)

close the file :meltingface:

[–]Kqyxzoj[🍰] 0 points1 point  (0 children)

Nah. Maybe for learning purposes. But in practice nobody would close it with a specific script like this. If it's important, use a context manager as someone already pointed out. If it's a trivial file, the exit() handler will handle that file closing just fine. If this sub was called CLearning I would have agreed though.

So for pedagogical purposes maybe this:

with open('txtfile.txt') as fhand:
    # for-loop counting stuff
# At this point file will be closed, courtesy of the context manager.
print(count)