Created
December 11, 2018 12:04
-
-
Save SureshKL/db55b6747ea853823ff5b0c23608c8b9 to your computer and use it in GitHub Desktop.
Get specified no. of lines from file
This file contains hidden or 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
def get_lines_in_chunks(file_path: str, chunks: int = 1) -> list: | |
"""Get specified no. of lines from file | |
:param chunks: No. of lines (Default=1) | |
:param file_path: Absolute path to file | |
:return: list of lines from the file | |
""" | |
with open(file_path) as f: | |
def _get_lines(chunks): | |
return [f.readline() for _ in range(chunks)] | |
while True: | |
lines = _get_lines(chunks) | |
if not any(lines): | |
return | |
yield lines | |
for lines in get_lines_in_chunks(__file__): | |
print(lines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment