Skip to content

Commit d8a6245

Browse files
authored
Uses 'with' instead of manually closing files
Uses 'with' statement when opening files to guarantee files are closed even when the process is interrupted.
1 parent cebbf56 commit d8a6245

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

other/word_patterns.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ def main():
1717
startTime = time.time()
1818
allPatterns = {}
1919

20-
fo = open('Dictionary.txt')
21-
wordList = fo.read().split('\n')
22-
fo.close()
20+
with open('Dictionary.txt') as fo:
21+
wordList = fo.read().split('\n')
2322

2423
for word in wordList:
2524
pattern = getWordPattern(word)
@@ -29,9 +28,9 @@ def main():
2928
else:
3029
allPatterns[pattern].append(word)
3130

32-
fo = open('Word Patterns.txt', 'w')
33-
fo.write(pprint.pformat(allPatterns))
34-
fo.close()
31+
with open('Word Patterns.txt', 'w') as fo:
32+
fo.write(pprint.pformat(allPatterns))
33+
3534
totalTime = round(time.time() - startTime, 2)
3635
print('Done! [', totalTime, 'seconds ]')
3736

0 commit comments

Comments
 (0)