Topic was automatically imported from the old Question2Answer platform.
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?
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()