use the with keyword to open the file, so that the file is properly closed

This commit is contained in:
Brian S. Stephan 2010-07-26 21:08:34 -05:00
parent 5927d361ef
commit c1da6cc052
1 changed files with 2 additions and 2 deletions

View File

@ -544,8 +544,8 @@ class FactFile(Module):
# open file
if os.path.isfile(filename):
# http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html
file = open(filename, 'r')
facts = file.readlines()
with open(filename, 'r') as file:
facts = file.readlines()
to_print = facts[random.randint(0, len(facts))]
# return text