Finding the length of a txt file without moving the cursor

Viewed 32
file = open("accounts.txt", "r")
num = len(file.readlines())
file = open("accounts.txt", "r")

this is my current solution but it just feels wrong, is there a better way of finding num without reopening the file to reset the cursor?

1 Answers

From Barmar's comment, this

file.seek(0)

is close enough to what I was looking for.

Related