From d8a62451031bcf67d5ce7e1e7fc730c65b7f649d Mon Sep 17 00:00:00 2001 From: edawine Date: Sun, 9 Oct 2016 00:30:05 +0700 Subject: [PATCH] Uses 'with' instead of manually closing files Uses 'with' statement when opening files to guarantee files are closed even when the process is interrupted. --- other/word_patterns.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/other/word_patterns.py b/other/word_patterns.py index b5bf0283..827c9fa8 100644 --- a/other/word_patterns.py +++ b/other/word_patterns.py @@ -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) @@ -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 ]')