Created
April 9, 2013 04:25
-
-
Save hyounggyu/5342958 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
# | |
# http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python | |
# | |
def readline_in_chunks(file_object, chunk_size=1024): | |
buf = '' | |
while True: | |
data = file_object.read(chunk_size) | |
if not data: | |
break | |
buf = buf + data | |
while True: | |
pos = buf.find('\n') | |
if pos < 0: | |
break | |
yield buf[:pos] | |
buf = buf[pos+1:] | |
def main(): | |
f = open('test.txt', 'r') | |
for line in readline_in_chunks(f): | |
print line | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment