mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Uses 'with' instead of manually closing files
Uses 'with' statement when opening files to guarantee files are closed even when the process is interrupted.
This commit is contained in:
parent
cebbf567ec
commit
d8a6245103
@ -17,9 +17,8 @@ def main():
|
||||
startTime = time.time()
|
||||
allPatterns = {}
|
||||
|
||||
fo = open('Dictionary.txt')
|
||||
with open('Dictionary.txt') as fo:
|
||||
wordList = fo.read().split('\n')
|
||||
fo.close()
|
||||
|
||||
for word in wordList:
|
||||
pattern = getWordPattern(word)
|
||||
@ -29,9 +28,9 @@ def main():
|
||||
else:
|
||||
allPatterns[pattern].append(word)
|
||||
|
||||
fo = open('Word Patterns.txt', 'w')
|
||||
with open('Word Patterns.txt', 'w') as fo:
|
||||
fo.write(pprint.pformat(allPatterns))
|
||||
fo.close()
|
||||
|
||||
totalTime = round(time.time() - startTime, 2)
|
||||
print('Done! [', totalTime, 'seconds ]')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user