Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79171 > unrolled thread

Re: Creating a counter

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2014-10-15 13:17 -0600
Last post2014-10-15 13:17 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Creating a counter Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-15 13:17 -0600

#79171 — Re: Creating a counter

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-15 13:17 -0600
SubjectRe: Creating a counter
Message-ID<mailman.14890.1413400675.18130.python-list@python.org>
On Wed, Oct 15, 2014 at 12:39 PM, Shiva
<shivaji_tn@yahoo.com.dmarc.invalid> wrote:
> 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")))

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web