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

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Yes, you can easily read a text file and count the line from it by using the .split() function and a for-loop.

Check the code snippet below:

file = open("work.txt","r") 
Count = 0  
Content = file.read() 
LineList = Content.split("\n") 
  
for n in LineList: 
    if n: 
        Count += 1          
print("This is the number of lines in the file") 
print(Count) 

This should help you. Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 25 views
25 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, 2020 LizzyM 6.1k points
0 votes
1 answer 16 views
16 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 9.6k points
0 votes
1 answer 10 views
10 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 9.6k points
1 vote
1 answer 35 views
35 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 4 views
4 views
Problem: I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file I was wondering if there is a smarter way to do that
asked 6 days ago Ali 5.1k points
0 votes
1 answer 7 views
7 views
Problem: lines() - read all lines of the file as a stream count() - returns the number of elements in the stream Here, if the file input.txt contains the following content: This is the question on Java Examples. The examples count number of lines in a file. , youhave used the java.nio.file package.
asked Feb 20 Ali 5.1k points
0 votes
1 answer 24 views
24 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 5 views
5 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 6 days ago Mashhoodch 9.6k points
0 votes
1 answer 7 views
7 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 6 days ago Mashhoodch 9.6k points
0 votes
1 answer 12 views
12 views
Problem: How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list. Don't use file.readlines() in a for-loop, a file object itself is enough: lines = [line.rstrip('\n') for line in file]
asked Feb 19 Mashhoodch 9.6k points