Last active
November 16, 2021 18:08
-
-
Save dcbriccetti/3e823c4beeaf82e2ae1898689c685cc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
# Really old way | |
wf2 = open('words.txt') | |
lines = wf2.readlines() | |
wf2.close() | |
# Old way, closes file automatically | |
with open('words.txt') as words_file: | |
lines = words_file.readlines() | |
words: list[str] = [line.rstrip() for line in lines] # List comprehension (super bonus material) | |
# A better way | |
all_text = pathlib.Path('words.txt').read_text() | |
words = all_text.split('\n') | |
print(words) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment