• Register
0 votes
21 views

I have a python file that contains too many lines of code.

I have written a code to count the total number of lines in a exact file (Don’t have any idea if it works).

My code:

def file_name(fname):
    with open(fname) as fn:
         for i, l in enumerate(fn):
             pass
    return i+1

Does the code give me the exact count ?

10 7 2
6,060 points

1 Answer

0 votes

Can you tell me the specific version of python you are using.

To count numbers of line in code or a file for python try:

num_lines = sum(1 dor line in open('your file name'))

If you map your memory this might be best solution for you .

Try this code:

from __future__ import with_statement
import time
import mmap
import random
from collections import defaultdict

def mapcount(filename):
    f = open(filename, "r+")
    buf = mmap.mmap(f.fileno(), 0)
    lines = 0
    readline = buf.readline
    while readline():
        lines += 1
    return lines

def simplecount(filename):
    lines = 0
    for line in open(filename):
        lines += 1
    return lines

def bufcount(filename):
    f = open(filename)                  
    lines = 0
    buf_size = 1024 * 1024
    read_f = f.read # loop optimization

    buf = read_f(buf_size)
    while buf:
        lines += buf.count('\n')
        buf = read_f(buf_size)

    return lines

def opcount(fname):
    with open(fname) as fn:
        for i, l in enumerate(fn):
            pass
    return i + 1


counts = defaultdict(list)

for i in range(5):
    for func in [mapcount, simplecount, bufcount, opcount]:
        start_time = time.time()
        assert func("big_file.txt") == 1209138
        counts[func].append(time.time() - start_time)

for key, vals in counts.items():
    print key.__name__, ":", sum(vals) / float(len(vals))

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 3 views
3 views asked 2 days ago Shaad Sh 800 points
1 vote
1 answer 20 views
20 views
Problem: I have a python file that contains too many lines of code. I have written a code to count the total number of lines in a exact file. My code: def file_name(fname): with open(fname) as fn: for i, l in enumerate(fn): pass return i+1 Does the code give me the exact count ? If not how can I solve this?
asked Apr 5 LizzyM 6.1k points
1 vote
1 answer 14 views
14 views
Problem: Well, I have a text file containing thousands of lines possibly. I am willing to give them a count (if possible). What is the easiest and efficient way to write a program to get number of lines in a file python? Please, keep it simple so that I can understand the code easily. Thanks.
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
I want to count lines, words and characters from a file. I'm having a problem counting the right amount of characters and words.
asked 6 days ago Ahsan 1.8k points
0 votes
1 answer 22 views
22 views
Problem: I have recently started learning the C Programming. Currently I am trying to write C programs. In this coding problem I am trying to count the number of characters, newlines and number of words. But I think there is some issue in the second while loop which I am unable to ... ();                 nc++;             }             nc++;         }     }     printf("%d %d %d",nl,nc,nw); }
asked Jun 16 Raphael Pacheco 4.9k points