So I'm not sure I understand your question based on all the answers provided. All you want to know is how to count the number of lines in a text file?
Yes I see you said "That have a word on the left of the colon", but why would you have a colon on a line with out any other text? Your saying that your program writes the text in such a way that it's "example":"example" right?
If that's the case you wouldn't have a colon on a line with out any other text, unless you intentionally input nothing which is still something.
def get_total_lines(self, path):
counter = 0
try:
if os.path.isfile(path):
with open(path, 'r') as inFile:
for i in enumerate(inFile):
counter = counter + 1
return str(counter)
elif not os.path.isfile(path):
print("Error: The file you're attempting to read does not exist, aborting reading process...")
except IOError as exception:
raise IOError('%s: %s' % (path, exception.strerror))
return None
That's got some basic form of error checking, here is one with out it.
def get_total_lines(self, path):
counter = 0
with open(path, 'r') as inFile:
for i in enumerate(inFile):
counter = counter + 1
return str(counter) # could be int or w/e else you want it to be
That will count the lines, if you want to strip off a word or the colon it can be easily adapted to do that.