Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79178 > unrolled thread
| Started by | MRAB <python@mrabarnett.plus.com> |
|---|---|
| First post | 2014-10-15 20:48 +0100 |
| Last post | 2014-10-15 20:48 +0100 |
| 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.
Re: Creating a counter MRAB <python@mrabarnett.plus.com> - 2014-10-15 20:48 +0100
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-10-15 20:48 +0100 |
| Subject | Re: Creating a counter |
| Message-ID | <mailman.14896.1413402701.18130.python-list@python.org> |
On 2014-10-15 20:24, Vincent Vande Vyvre wrote:
> Le 15/10/2014 20:39, Shiva a écrit :
>> 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.
>>
> Try this:
>
> >>> def counter(x):
> ... for i in range(x):
> ... print "\rProgress .............. %s" % i,
> ... sys.stdout.flush()
> ... time.sleep(1)
> ...
> >>> 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
end, leaving the cursor just after the last thing printed:
>>> def counter(x):
... for i in range(x):
... print("\rProgress .............. %s" % i, end="")
... sys.stdout.flush()
... time.sleep(1)
...
>>> counter(5)
The "\r" moves the cursor back to the start of the line so that it
overwrites what was printed last time.
Back to top | Article view | comp.lang.python
csiph-web