Skip to content

Commit df87984

Browse files
authored
Merge pull request TheAlgorithms#35 from edawine/patch-2
Uses 'with' instead of manually closing files
2 parents cebbf56 + d8a6245 commit df87984

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

‎other/word_patterns.py‎

Lines changed: 5 additions & 6 deletions
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+
withopen('Dictionary.txt') asfo:
21+
wordList=fo.read().split('\n')
2322

2423
forwordinwordList:
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+
withopen('Word Patterns.txt', 'w')asfo:
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)