How to count lines in a text file ?

Ling Lee janimal at mail.trillegaarden.dk
Mon Sep 20 09:25:02 EDT 2004


Oh I just did it.

Just used the line:

print "%d lines in your choosen file" % len(open("test.txt").readlines())

Thanks though :)


"Ling Lee" <janimal at mail.trillegaarden.dk> wrote in message 
news:414ed896$0$26378$ba624c82 at nntp02.dk.telia.net...
> Hi all.
>
> I'm trying to write a program that:
> 1) Ask me what file I want to count number of lines in, and then counts 
> the lines and writes the answear out.
>
> 2) I made the first part like this:
>
> in_file = raw_input("What is the name of the file you want to open: ")
> in_file = open("test.txt","r")
> text = in_file.read()
>
> 3) I think that I have to use a for loop ( something like: for line in 
> text: count +=1)
> Or maybee I have to do create a def: something like: ( def loop(line, 
> count)), but not sure how to do this properly.
> And then perhaps use the readlines() function, but again not quite sure 
> how to do this. So do one of you have a good idea.
>
> Thanks for all help
>
>
> 





More information about the Python-list mailing list