-4

I just want to count the file lines.

I have googoled around and found two ways but in vane

test = open('./log/test.csv','a')

if sum(1 for line in test) == 0:
    print("no line")

shows

io.UnsupportedOperation: not readable

second way

test = open('./log/test.csv','a')

if len(test.readlines()) == 0:
    print("no line")

it shows error too.

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 2
    Possible duplicate of [How to get line count cheaply in Python?](https://stackoverflow.com/questions/845058/how-to-get-line-count-cheaply-in-python) – Chris_Rands Jul 26 '17 at 08:49
  • You are opening up the file in 'append' mode. How are you expecting to read from it? – SiHa Jul 26 '17 at 09:03

2 Answers2

1

try doing:

test = open('./log/test.csv', 'r')

The issue could be you don't have it on read mode

dheiberg
  • 1,914
  • 14
  • 18
  • AH I see, I wanted to append the lines to this file, so I use `a` value and I thought `a` includes `r` function, now I open file once by `r` mode and then open again with 'a' mode to append lines – whitebear Jul 26 '17 at 08:50
0

Try removing 'a' in your open function:

with open('file.csv') as f:
    print(len(list(enumerate(f))))
Rahul
  • 10,830
  • 4
  • 53
  • 88