#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    12
    Rep Power
    0

    Printing to the same line


    Hi All --

    I have a loop and want to print something to the screen during each iteration of the loop.

    However, I do not want to fill the screen, and want to overwrite the printed-to-screen output with each subsequent iteration of the loop (i.e. I want to be able to print to one line only, overwriting the previous print on each loop iteration).

    Is this doable????

    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    You would do this by importing the os module
    the
    os.system("cls") if it is a windows, I dont know for other platforms...

    And there are toehr ways but they are not worth looking at for this.
    Those people who think they know everything are a great annoyance to those of us who do.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    12
    Rep Power
    0
    Originally Posted by monkeyman23555
    You would do this by importing the os module
    the
    os.system("cls") if it is a windows, I dont know for other platforms...

    And there are toehr ways but they are not worth looking at for this.
    Thanks - works perfectly!!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    no problem
    Those people who think they know everything are a great annoyance to those of us who do.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    10
    or you can use sys.stdout.write and sys.stdout.flush which doesn't clear the whole screen:

    Code:
    import sys, time
    
    for n in range(10):
        sys.stdout.write("This is line "+str(n)+"\r")
        sys.stdout.flush()
        time.sleep(1)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    yes but there is not really a difference except that a line is cleared. and the other way is easier to understand
    Those people who think they know everything are a great annoyance to those of us who do.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    10
    Yes, but the value returned by os.system() is system-dependent (edit: and linux or other os than windows doesn't have the command "cls" as far as I know)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    ya it does not work on linux or mac or anyother platform...

    But there are commands that clean the console window
    Those people who think they know everything are a great annoyance to those of us who do.

IMN logo majestic logo threadwatch logo seochat tools logo