0

I want to know how properly solve a problem similar to this:

Python Progress Bar

With one exception, my script is using a new line character, and it looks like this:

#!/usr/bin/env python

import time
import sys
import random

total = 100
progress = 0

for i in xrange(0, 100):
    time.sleep(0.01)
    progress += 1
    random_word = 'a' * random.randint(1, 10)
    random_int = random.randint(4, 12)
    how_many_characters = 99
    sys.stdout.write("%d/100 %s\n" % (progress, random_word * random_int))
    sys.stdout.write("\b" * how_many_characters)

While this line

sys.stdout.write("\b" * how_many_characters)

is the problem.

How to determine where previous line starts and how long is it? Or how to move to first character of previous line? Or what else I can do? What I want to achieve is to change already displayed text

1/100 Crazy white fox jumping over fence\n

Into:

2/100 Crazy white fox jumping over fence\n

Extra requirements:

  • I cannot remove that trailing \n from original sentence
  • I know that progress bar is displayed at beginning of previous line

Edit.

Improved question to remove ambiguity.

Community
  • 1
  • 1
Drachenfels
  • 3,037
  • 2
  • 32
  • 47
  • What's wrong with it and what do you want to do? – tayfun Feb 05 '14 at 12:19
  • Presumably you want to print a character number of times in an increasing way: `sys.stdout.write("-" * progress)` but `\b` is the backspace character isn't it? – tayfun Feb 05 '14 at 12:22
  • Execute this shell script I've written. It's updating progress 1/100, 2/100, 3/100 and etc. I want to update old value not to print new one. This approach works as long as your console is around ~90 characters per line. – Drachenfels Feb 05 '14 at 12:34
  • OK got it, thanks for clarification. – tayfun Feb 05 '14 at 12:39

2 Answers2

0

try this one :)

import time
import sys

total = 100
progress = 0

for i in xrange(0, 9):
    time.sleep(0.01)
    progress += 1
    sys.stdout.write("\b"*5)
    sys.stdout.write("%d/100" % progress)
    sys.stdout.flush()
for i in xrange(10, 100):
    time.sleep(0.01)
    progress += 1
    sys.stdout.write("\b"*6)
    sys.stdout.write("%d/100" % progress)
    sys.stdout.flush()

time.sleep(0.01)
progress += 1
sys.stdout.write("\b"*7)
sys.stdout.write("%d/100" % progress)
sys.stdout.flush()


sys.stdout.write("\n")
Zhen Zhang
  • 1,052
  • 1
  • 11
  • 18
  • I really can't understand want effect do you want...maybe you can use some system function to get the width of console and act like a real '\n', i'm sorry – Zhen Zhang Feb 05 '14 at 13:35
  • Script I put into original question is working fine for me as long as shell is 91 character long. I can read the shell size from system (I guess) but it will couple my code with shell and operating system (I guess). So what I am looking is some kind of neat way to achieve that. Maybe there is a command like first character of the line or remove line or go up one line, that maybe we have something more than \b or there is another way I can achieve the same. – Drachenfels Feb 06 '14 at 11:40
0

I understand that you want to remove previous progress indicator from comments.

You should remove the newline character, replace sys.stdout.write("%d/100\n" % progress) with sys.stdout.write("%d/100" % progress) so that it can be removed.

tayfun
  • 3,065
  • 1
  • 19
  • 23