Seeking to the start of a line in a file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hotmailking

I want to use the File.seek() function to seek to the beginning of a line in a file. Instead of doing seek(2) and seeking to the third character, I want it to seek to the start of the third line. How do I do something like this?

:bust_in_silhouette: Reply From: hotmailking

Figured out a way to do it. Not sure if it’s the best way, but it’s all I can think of:

I pick a random number from 0 to the file’s number of lines, then loop from 0 to that number and use the “get_line()” function each time. This brings the cursor to the start of the desired line;

for i in range(0, randi() % lineCount):
	file.get_line()