Created
August 30, 2017 18:54
-
-
Save Yunkou/0a5f41bb553f765560e0b056c443a6a0 to your computer and use it in GitHub Desktop.
File reading with ‘with’
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
# The bad way | |
f = open('pimpin-aint-easy.txt') | |
text = f.read() | |
for line in text.split('\n'): | |
print(line) | |
f.close() | |
# The good way | |
with open('pimpin-aint-easy.txt') as f: | |
for line in f: | |
print(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment