1

Possible Duplicate:
How to create a spinning command line cursor using python?
python 3 IDLE progressbar/loadingbar

I want to print the output as below:

Percentage: 10%

and only the percentage keeps changing to 20, 30, 40 so on in the same position. Overwriting 10%. I saw many of the posts on stackoverflow. But none of them work right in Python 3.

I tried using \r, \c and even \b but does not work from within print() or sys.stdout()

I am trying this using IDLE and running the program from Python Shell by pressing F5. The environment is Windows.

Please help. Thanks

Community
  • 1
  • 1
Romaan
  • 2,645
  • 5
  • 32
  • 63
  • do you want a solution for unix, windows, or both? – SingleNegationElimination Nov 26 '12 at 23:46
  • Windows solution will suffice. – Romaan Nov 26 '12 at 23:47
  • http://stackoverflow.com/questions/4995733/how-to-create-a-spinning-command-line-cursor-using-python – SingleNegationElimination Nov 26 '12 at 23:51
  • @TokenMacGuy Na, it printed junk character like this \|/-\|/-\|/-\| – Romaan Nov 26 '12 at 23:52
  • Not sure I understand what is different, I pasted that code and produced the desired results, windows 7 – SingleNegationElimination Nov 26 '12 at 23:54
  • @TokenMacGuy, sys.stdout.write('\b') this line is actually producing wierd character on my terminal, not sure why. I can to paste a screen shot of the output. I dont know how paste the screen shot in stackoverflow either. But I am not getting the characters replaced as they get printed. – Romaan Nov 26 '12 at 23:56
  • 1
    The IDLE console doesn't support using `\b` and `\r` to backtrack and overwrite characters that have been printed. Such techniques will work if you run python.exe from a cmd.exe window. – Brian L Nov 26 '12 at 23:58
  • @BrianL , feel like you are right. So what might be the solution...? – Romaan Nov 26 '12 at 23:58
  • Either run your program from a cmd window, or put up with the fact that your output is going to be on several lines? Or turn your program into a GUI program so that you actually have control over what is on the screen... – Brian L Nov 27 '12 at 00:01
  • If there's a way for your program to tell whether it's running from within IDLE or not -- and there probably is -- just create two versions of you progress indicator and use the appropriate one. See this [answer](http://stackoverflow.com/questions/7049481/detecting-the-presence-of-idle-how-to-tell-if-file-isnt-set). – martineau Nov 27 '12 at 03:11

1 Answers1

4

IDLE does not provide a true TTY/terminal. Sorry.

Test your program by running it from the windows command line:

C:\PythonXX\python.exe path\to\script.py

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304