Created
April 28, 2020 09:20
-
-
Save nimpy/de8b26ac2253556e39e4d37aba49309f to your computer and use it in GitHub Desktop.
Read a file line by line
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
file = open('path/to/file', 'r') | |
count = 0 | |
while True: | |
line = file.readline() | |
if not line: | |
break | |
# do something here | |
count += 1 | |
file.close() |
I should be using the with statement. See: https://stackoverflow.com/a/48964410/4031135
TODO: update this gist accordingly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So that I don't have to look at a 66 ads on that Python tutorial website every time I want to read a file
(Also, don't know if this is the best way to read a file? But should be good enough for most of (my) use-cases)