0

I am trying to count the lines in a file but the intellisense is throwing a warning that the variable line is never used. This makes me assume there is a better way of doing this.

Please note I am not asking how to efficiently count the rows!

with open(FILE, newline='') as f:
  reader = csv.reader(f)
  for line in reader:
    counter = counter + 1

Is there a better way of doing this?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
  • 1
    Does this answer your question? [How to get line count of a large file cheaply in Python?](https://stackoverflow.com/questions/845058/how-to-get-line-count-of-a-large-file-cheaply-in-python) – Brian61354270 Feb 14 '21 at 16:49
  • @Brian it does not. It's not even close to what I asked. – SomeDutchGuy Feb 14 '21 at 16:54
  • My apologies. Given the title of your question "Counting lines in a file", I read "this" as meaning "how to count lines in a file", not "how to name unused variables". Anyhow, the correct duplicate would be https://stackoverflow.com/questions/5477134/how-can-i-get-around-declaring-an-unused-variable-in-a-for-loop – Brian61354270 Feb 14 '21 at 16:58
  • @Brian I edited the title to prevent confusion. – SomeDutchGuy Feb 14 '21 at 16:59

1 Answers1

4

The idiomatic way of indicating a variable is unused is to use _.

  for _ in reader:
    # ...
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328