Discussion:
Creating a counter
Shiva
2014-10-15 18:39:09 UTC
Permalink
Hi,

I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.

How do I implement it?

If I use a print() within a if condition statement - and execute the script
in terminal - for each find, the print() prints in new line instead of a
constantly incrementing counter.

Thanks,
Shiva
Ian Kelly
2014-10-15 19:17:11 UTC
Permalink
On Wed, Oct 15, 2014 at 12:39 PM, Shiva
Post by Shiva
Hi,
I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.
How do I implement it?
If I use a print() within a if condition statement - and execute the script
in terminal - for each find, the print() prints in new line instead of a
constantly incrementing counter.
Before the loop, initialize the counter to 0:

counter = 0

Inside the loop, increment the counter:

counter = counter + 1 # can be shortened to: counter += 1

Alternatively, if you have all the strings you want to count in a
list, you can just check the length of the list:

print(len(os.listdir("/some/directory")))
Terry Reedy
2014-10-15 19:26:52 UTC
Permalink
Post by Shiva
Hi,
I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.
How do I implement it?
n=0 before the look and
n += 1 withing the loop
--
Terry Jan Reedy
Vincent Vande Vyvre
2014-10-15 19:24:27 UTC
Permalink
Post by Shiva
Hi,
I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.
How do I implement it?
If I use a print() within a if condition statement - and execute the script
in terminal - for each find, the print() prints in new line instead of a
constantly incrementing counter.
Thanks,
Shiva
... for i in range(x):
... print "\rProgress .............. %s" % i,
... sys.stdout.flush()
... time.sleep(1)
...
Post by Shiva
counter(5)
Vincent.
MRAB
2014-10-15 19:48:30 UTC
Permalink
Post by Vincent Vande Vyvre
Post by Shiva
Hi,
I am trying to search a string through files in a directory -
however while Python script works on it and writes a log - I want
to present the user with count of number of strings found. So it
should increment for each string found.
How do I implement it?
If I use a print() within a if condition statement - and execute
the script in terminal - for each find, the print() prints in new
line instead of a constantly incrementing counter.
... print "\rProgress .............. %s" % i,
... sys.stdout.flush()
... time.sleep(1)
...
Post by Shiva
counter(5)
In Python 3, you can specify what to do at the end of the print.
Normally it prints a newline, but you can make it print nothing at the
... for i in range(x):
... print("\rProgress .............. %s" % i, end="")
... sys.stdout.flush()
... time.sleep(1)
...
Post by Vincent Vande Vyvre
Post by Shiva
counter(5)
The "\r" moves the cursor back to the start of the line so that it
overwrites what was printed last time.
Gary Herron
2014-10-15 20:04:03 UTC
Permalink
Post by Shiva
Hi,
I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.
How do I implement it?
If I use a print() within a if condition statement - and execute the script
in terminal - for each find, the print() prints in new line instead of a
constantly incrementing counter.
Thanks,
Shiva
The question seems confuse the word "increment".

If you are asking how to count, plenty of responders have answered
count = count+1
and similar.

However, the second paragraphs seems to use the word "increment" to mean
displaying the counter on a single line overwriting itself instead of a
scrolling list of values one-per-line. The solution may depend on what
is displaying the value as you write it out, but if it's a
terminal/command-prompt /console-window, then you want the write to
finish with a carriage-return alone instead of a
carriage-return/new-line combination. In Python that is represented
with a \r instead of a \n.

Python2:
print counter, '\r', # The ending comma means do NOT output the usual \n

Python3:
print(counter, end='\r')

Gary Herron
Shiva
2014-10-16 10:20:59 UTC
Permalink
Post by Gary Herron
print(counter, end='\r')
Gary Herron
Thanks, that is what I was looking up - \r carriage return without linefeed.

Thanks again!
Shiva
Simon Kennedy
2014-10-16 13:44:09 UTC
Permalink
Post by Shiva
I am trying to search a string through files in a directory - however while
Python script works on it and writes a log - I want to present the user with
count of number of strings found. So it should increment for each string found.
You may ignore the following if you are just starting out on your Python learning odyssey but if you're past the early stages and looking to try something different then read on.

When you looked through the other answers and found a solution you're happy with that does not use the standard library you can look through the documentation and find a stdlib (https://docs.python.org/2.7/library/index.html) provided way that may be slightly more elegant for counting strings.
Post by Shiva
from collections import defaultdict
counter = defaultdict(int)
counter['string1'] += 1
counter['string2'] += 1
counter['string1'] += 1
print('Count string1={string1}, string2={string2}'.format(**counter), end='\r')
Count string1=2, string2=1

https://docs.python.org/3/library/collections.html#collections.defaultdict

In the above code defaultdict is like a dictionary but if a key is not found then it uses the `int` factory parameter to add a value that is an integer with a value of 0 (which is what executing ``int()`` returns if you do it in the interactive Python prompt) for the dictionary key you're trying to access.
Post by Shiva
from collections import Counter
counter = Counter()
s = 'string1 string2 string1'
s.split()
['string1', 'string2', 'string1']
Post by Shiva
counter.update(s.split())
print('Count string1={string1}, string2={string2}'.format(**counter), end='\r')
Count string1=2, string2=1

https://docs.python.org/2.7/library/collections.html#collections.Counter
Ian Kelly
2014-10-16 14:04:25 UTC
Permalink
Post by Simon Kennedy
When you looked through the other answers and found a solution you're happy with that does not use the standard library you can look through the documentation and find a stdlib (https://docs.python.org/2.7/library/index.html) provided way that may be slightly more elegant for counting strings.
I would have suggested a Counter if I thought it fit the OP's use
case. If you're listing directory contents, you're not going to have
any repeated strings, so all the counts will be 1, and your Counter
might as well be a list, which is what you got from calling
os.listdir() in the first place. So I was more inclined to think that
the OP was only trying to count one thing.

In any case, the question turned out to actually be about printing,
not counting.
Simon Kennedy
2014-10-16 14:12:07 UTC
Permalink
Post by Ian Kelly
I would have suggested a Counter if I thought it fit the OP's use
case. If you're listing directory contents, you're not going to have
any repeated strings, so all the counts will be 1, and your Counter
might as well be a list, which is what you got from calling
os.listdir() in the first place. So I was more inclined to think that
the OP was only trying to count one thing.
I read the OP's question as he was looking for the string in the contents of each file in the directory not in the file names themselves but as you say...
Post by Ian Kelly
In any case, the question turned out to actually be about printing,
not counting.
Loading...