Python count lines of text file
Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Python.
More related articles in Python. Python program to check if a string is palindrome or not Check if element exists in list in Python Programs for printing pyramid patterns in Python.
How many words of 3 vowels and 6 consonants can be formed taken from 5 vowels and 10 consonants? Python program to Count the Number of occurrences of a key-value pair in a text file.
Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Python. More related articles in Python. Improve this question. You can avoid that slash-escaped quote by using triple-quotes for the string… see my answer. Add a comment. Active Oldest Votes. Improve this answer. I just checked execution times: 0. But this is actually looping quite a bit - each content. To take care of cases like!!
In this case I miss having the detailed output per each character. But I take note on the efficiency — neif. What detailed output?
You can still use pprint on a Counter, and for every character not there you can be sure the count is zero. Then, you can find any occurrence of character from the dict.
This reduce complexity of the program. TheExorcist TheExorcist 1, 1 1 gold badge 18 18 silver badges 23 23 bronze badges. Good idea, unfortunately this does not work on Windows though. Believe me you will thank me one day. I just considered it noteworthy that this will only work on windows. As the OP did not mention his platform and in case anyone pops on this solution via google and copies it unaware of the limitations a Windows system might have , I wanted to add the note.
You can't save output of os. AnSe you are correct but question is not asked whether it saves or not. I guess you are understanding the context. Show 3 more comments. Scott Persinger Scott Persinger 3, 2 2 gold badges 18 18 silver badges 10 10 bronze badges.
A bug: buffer in the last round might not be clean. Just to complete the above methods I tried a variant with the fileinput module: import fileinput as fi def filecount fname : for line in fi. BandGap BandGap 1, 3 3 gold badges 19 19 silver badges 25 25 bronze badges. As for me this variant will be the fastest:!
SilentGhost k 61 61 gold badges silver badges bronze badges. Mykola Kharechko Mykola Kharechko 2, 3 3 gold badges 29 29 silver badges 39 39 bronze badges. But is it? You state that it will be the fastest and then state that you haven't tested it.
Not very scientific eh? See solution and stats provided by Ryan Ginstrom answer below. Also check out JF Sebastian's comment and link on the same answer. Show 1 more comment.
Dummy Dummy 41 1 1 bronze badge. Maybe also explain or add in comment in the code what you changed and what for ;. Might give people some more inside in your code much easier rather than "parsing" the code in the brain.
This code is shorter and clearer. Texom Texom 4, 2 2 gold badges 15 15 silver badges 15 15 bronze badges. You should also close the file. It will load the whole file into memory. If one wants to get the line count cheaply in Python in Linux, I recommend this method: import os print os. Hope this may help. Lerner Zhang Lerner Zhang 4, 1 1 gold badge 35 35 silver badges 53 53 bronze badges.
Innat Mohideen bin Mohammed Mohideen bin Mohammed In this example file is not closed. Andrew Jaffe Andrew Jaffe It's probably possible to create an iterator that does this, but I think it would be equivalent to your solution.
This give the count -1 of the true value. Optional second argument for enumerate is start count according to docs. How about this? Could you please explain what is wrong with it if you think it is wrong?
0コメント