1
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
    You don't have to reopen the file to reset the cursor, you can use `file.seek(0)` – Barmar Sep 08 '22 at 04:28
  • 1
    There's no way to count lines without reading the file. If you wanted the length in bytes there are ways, but not lines. – Barmar Sep 08 '22 at 04:29
  • Does this answer your question? [Why can't I call read() twice on an open file?](https://stackoverflow.com/questions/3906137/why-cant-i-call-read-twice-on-an-open-file) – gre_gor Sep 13 '22 at 05:10

1 Answers1

0

From Barmar's comment, this

file.seek(0)

is close enough to what I was looking for.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135