Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Last active November 16, 2021 18:08
Show Gist options
  • Save dcbriccetti/3e823c4beeaf82e2ae1898689c685cc6 to your computer and use it in GitHub Desktop.
Save dcbriccetti/3e823c4beeaf82e2ae1898689c685cc6 to your computer and use it in GitHub Desktop.
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