|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
counting lines
Can some one give me a sample on how to count lines in a txt file.. or etc
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
||||
|
||||
|
never mind i got it working using some php skils =)
sort off ... anyways check it Code:
fopen = open("1.txt", "r")
read = fopen.read()
count = 0
for enter in read:
if enter == "\n":
count = count + 1
print count
is this the best way to do it.. or is there a better way? |
|
#3
|
||||
|
||||
|
As with most things, Python makes it very easy, not too mention quite attractive.. all you need to do this is these 15 lines of code, oh wait, this is only one line
![]() Code:
count = len(file('file.txt', 'r').readlines())
Mark. |
|
#4
|
||||
|
||||
|
Oh while i think about it heres another ways too do this (although id recommend the first one), i've included this one because it demenstrates the use of the string types count(string) method (which can be pretty useful sometimes!)
Code:
count = file('file.txt', 'r').read().count('\n')
Note: this may not always return the same as the first example since example one counts the number of lines while this one counts the number of '\n' in the file. Mark. |
|
#5
|
||||
|
||||
|
hey thanx =)
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > counting lines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|