You can use the with statement to automatically close the file when it's no longer needed: with open('myfile.txt', 'r') as f: # do something with the file