• Register
1 vote
28 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?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

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))

Hope this helps you. 

11 6 4
34,950 points

Related questions

1 vote
1 answer 17 views
17 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, 2020 Gavin 15.3k points
0 votes
1 answer 17 views
17 views
Problem: I need to get a line count of a large file (hundreds of thousands of lines) in python. What is the most efficient way both memory- and time-wise? At the moment I do: def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1 is it possible to do any better?
asked Feb 20 Mashhoodch 13k points
0 votes
1 answer 55 views
55 views
Problem: for reasonably sized files, is to read the file as a list of lines so that the count of lines is the length of the list. If the file’s path is in a string bound to the thefilepath variable, that’s just: count = len(open(thefilepath).readlines ... humongous files, a loop using the xreadlines method always works: count = 0 for line in open(thefilepath).xreadlines( ): count += 1
asked Jan 23 Mashhoodch 13k points
1 vote
1 answer 54 views
54 views
Problem: I am a new student of c++ and learning new things day by day. 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 get number of lines in a file c++? Please, keep it simple so that I can understand the code easily. Thanks.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 32 views
32 views
Problem Hi, How can I get to know number of lines in a file on Linux?
asked Oct 31, 2020 hashq 1.1k points
0 votes
1 answer 6 views
6 views
Problem: I am facing problem with this: Get number of items in list python.
asked Mar 20 PkGuy 14k points
0 votes
1 answer 8 views
8 views
Problem: how to I Solve this: count number of elements in list python HELP! I need help understanding this: count number of elements in list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 7 Mashhoodch 13k points
0 votes
1 answer 11 views
11 views
Problem: Python List count() The count() method returns the number of times the specified element appears in the list. Return value from count() The count() method returns the number of times element appears in the list. I want to learn count numbers with these two methods?
asked Feb 23 Mashhoodch 13k points
0 votes
1 answer 17 views
17 views
Problem: I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*')), but that also counts the directory itself as a file. Is there any way to count only the files in a directory?
asked Feb 23 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem If i want to write a source code where counts how many lines are present in a text file, Anyone can help me.
asked 6 days ago Subhendu 4.5k points