Skip to content

Instantly share code, notes, and snippets.

@SureshKL
Created December 11, 2018 12:04
Show Gist options
  • Save SureshKL/db55b6747ea853823ff5b0c23608c8b9 to your computer and use it in GitHub Desktop.
Save SureshKL/db55b6747ea853823ff5b0c23608c8b9 to your computer and use it in GitHub Desktop.
Get specified no. of lines from file
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