Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.040 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'initialize': 0.07; 'string': 0.09; 'python': 0.11; 'increment': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'trying': 0.19; 'skip:p 40': 0.19; 'script': 0.25; '15,': 0.26; 'header:In-Reply-To:1': 0.27; 'list:': 0.30; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; 'prints': 0.31; 'received:google.com': 0.35; 'found.': 0.36; 'hi,': 0.36; 'should': 0.36; 'implement': 0.38; 'writes': 0.38; 'to:addr :python-list': 0.38; 'files': 0.38; 'list,': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'length': 0.61; 'new': 0.61; 'within': 0.65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=5Yort2jsQIn8CblvttvxCPUZO5HKNqmci3xK9F1O0rA=; b=gAiW0IlmKd5KraaBzS+GvozFHEvTkJ27ciKLqh+3u7uk03zkfKxLTLsZ02/BCmbwYu n3t0Qb+6gvKXqZP44P0Hz6wsHkAnSyXXqCQV1PJdNGQs8V6jbSxrADOVDxE8XywPcpce jV6zJd0ocGBmnyJHeyC0HcleRcOZRTrYHESynKAwBjPBzjyIfJk6czUkYY+WUhthmuH/ oE7/vRteGX3U3XkkKKOwh5ITT8W43DN+ZwhxGi8I5QnhmQygIxLsc1d6Mr9rCS99csuL aqr2lKvqlF2At4mV0iZFcSMA4h+amXGxHdJH7FowETz+jHkg8OXTDRy+I0fgiZb2o6Pj IOyw== X-Received: by 10.66.252.193 with SMTP id zu1mr4415482pac.153.1413400671756; Wed, 15 Oct 2014 12:17:51 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 15 Oct 2014 13:17:11 -0600 Subject: Re: Creating a counter To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413400675 news.xs4all.nl 2944 [2001:888:2000:d::a6]:60975 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79171 On Wed, Oct 15, 2014 at 12:39 PM, Shiva 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")))