Problems with sys.stout.flush()
Joel Ross
joelc at cognyx.com
Sat May 23 10:44:21 EDT 2009
Carl Banks wrote:
> On May 23, 3:49 am, Joel Ross <jo... at cognyx.com> wrote:
>> def progressbar(self, number, total, char):
>>
>> percentage = float(number*100)/total
>> percentage = int(round(percentage))
>> percentage = int(100 - percentage)
>> self.f=sys.stdout
>> if percentage > 0:
>> char = char * percentage
>> self.f.write(char)
>> self.f.flush()
>> sleep(0.2)
> [snip]
>> So when it prints a progress at 50% it will print 50 (*) characters and
>> if the next progress is 51% it will print 51 (*) characters including
>> the last 50 (*) characters, so instead on ending up with 100 (*)
>> characters I end up with a shit load of them depending on how many lines
>> I pass to the progressbar() function.
>
> Here's why: when you write "char = char * percentage", you are setting
> char to a string, so on the next iteration char is the string you
> printed last time. Thus char grows factorially with iterations. Not
> good.
>
> Instead, change char to some other name so you don't ever overwrite
> it; char should always be just one character.
>
> bar = char * percentage
> self.f.write(bar)
>
>
> Carl Banks
Still having the same problem if I pass it 1000 lines it will printout
1000 asterisks when I say lines I mean the argument <number> for the
progress() function. I only want to printout 100 asterisks no matter how
many lines I pass to the progress() function, that's why I need the
printout to overwrite the last printout instead of appending to the last
printout. I know there must be a way to do it. I'll keep trying and
eventually get it. Any ideas would be helpful
jross
More information about the Python-list
mailing list