Skip to content

Commit

Permalink
Uses 'with' instead of manually closing files
Browse files Browse the repository at this point in the history
Uses 'with' statement when opening files to guarantee files are closed even when the process is interrupted.
  • Loading branch information
essut authored Oct 8, 2016
1 parent cebbf56 commit d8a6245
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions other/word_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def main():
startTime = time.time()
allPatterns = {}

fo = open('Dictionary.txt')
wordList = fo.read().split('\n')
fo.close()
with open('Dictionary.txt') as fo:
wordList = fo.read().split('\n')

for word in wordList:
pattern = getWordPattern(word)
Expand All @@ -29,9 +28,9 @@ def main():
else:
allPatterns[pattern].append(word)

fo = open('Word Patterns.txt', 'w')
fo.write(pprint.pformat(allPatterns))
fo.close()
with open('Word Patterns.txt', 'w') as fo:
fo.write(pprint.pformat(allPatterns))

totalTime = round(time.time() - startTime, 2)
print('Done! [', totalTime, 'seconds ]')

Expand Down

0 comments on commit d8a6245

Please sign in to comment.