Printing to console (No Scroll)

Discussion in 'Python' started by Totte Karlsson, Jan 14, 2004.

  1. Hi,
    How can I print to the console without having it scrolling to a new line for
    each print statement?
    I want to print a count down in the console, but for each count it scrolls
    the screen (of course).

    Is there another way?

    Here is the simple script for now

    print "Closing window in :"
    for second in range(10):
    time.sleep(1)
    print `10-second` +" seconds"

    thanks
    /totte
    Totte Karlsson, Jan 14, 2004
    #1
    1. Advertising

  2. > How can I print to the console without having it scrolling to a new line
    > for each print statement?
    > I want to print a count down in the console, but for each count it scrolls
    > the screen (of course).


    Use ncurses.

    Diez
    Diez B. Roggisch, Jan 14, 2004
    #2
    1. Advertising

  3. Totte Karlsson

    Joe Francia Guest

    Totte Karlsson wrote:
    > Hi,
    > How can I print to the console without having it scrolling to a new line for
    > each print statement?
    > I want to print a count down in the console, but for each count it scrolls
    > the screen (of course).
    >
    > Is there another way?
    >
    > Here is the simple script for now
    >
    > print "Closing window in :"
    > for second in range(10):
    > time.sleep(1)
    > print `10-second` +" seconds"
    >
    > thanks
    > /totte


    This works for me:

    import time, sys
    for second in range(10):
    time.sleep(1)
    sys.stdout.write(`10-second` + " seconds ")
    sys.stdout.flush()
    Joe Francia, Jan 14, 2004
    #3
  4. Any alternatives to ncurses? It seems like a overkill for this...
    cheers
    /totte


    "Diez B. Roggisch" <> wrote in message
    news:bu41k5$4in$00$-online.com...
    > > How can I print to the console without having it scrolling to a new line
    > > for each print statement?
    > > I want to print a count down in the console, but for each count it

    scrolls
    > > the screen (of course).

    >
    > Use ncurses.
    >
    > Diez
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Totte Karlsson, Jan 14, 2004
    #4
  5. Totte Karlsson wrote:

    > Any alternatives to ncurses? It seems like a overkill for this...


    Maybe you can use sys.stdout.write in conjunction with control-codes for
    moving back the cursor to column one. But you'll have to lookup these for
    yourself :)

    Diez
    Diez B. Roggisch, Jan 14, 2004
    #5
  6. Totte Karlsson

    Derek Guest

    "Diez B. Roggisch" wrote
    > > Any alternatives to ncurses? It seems like a overkill for this...

    >
    > Maybe you can use sys.stdout.write in conjunction with control-
    > codes for moving back the cursor to column one. But you'll have
    > to lookup these for yourself :)


    I think \r is the control code (at least if you want to go back to the
    start of the line):

    import time, sys
    for second in range(10):
    time.sleep(1)
    sys.stdout.write(`10-second` + " seconds \r")
    sys.stdout.flush()
    Derek, Jan 14, 2004
    #6
  7. Great, thanks for all help! I'll try these things

    "Derek" <> wrote in message
    news:bu45kp$d99m3$-berlin.de...
    > "Diez B. Roggisch" wrote
    > > > Any alternatives to ncurses? It seems like a overkill for this...

    > >
    > > Maybe you can use sys.stdout.write in conjunction with control-
    > > codes for moving back the cursor to column one. But you'll have
    > > to lookup these for yourself :)

    >
    > I think \r is the control code (at least if you want to go back to the
    > start of the line):
    >
    > import time, sys
    > for second in range(10):
    > time.sleep(1)
    > sys.stdout.write(`10-second` + " seconds \r")
    > sys.stdout.flush()
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Totte Karlsson, Jan 15, 2004
    #7
  8. The cheesy dirt simple way:

    print "Closing window in :"
    for second in range(10):
    time.sleep(1)
    #print enough spaces to cover up the last message.
    print " "*20 + "\r",
    print `10-second` +" seconds",

    Note the comma at the end of each print statement.
    That causes python not to output a newline.

    HTH

    Sam Walters.

    --
    Never forget the halloween documents.
    http://www.opensource.org/halloween/
    """ Where will Microsoft try to drag you today?
    Do you really want to go there?"""
    Samuel Walters, Jan 15, 2004
    #8
  9. Samuel Walters fed this fish to the penguins on Wednesday 14 January
    2004 21:30 pm:

    > print " "*20 + "\r",
    > print `10-second` +" seconds",
    >
    > Note the comma at the end of each print statement.
    > That causes python not to output a newline.
    >

    Don't you need a \r on the second print line also? Otherwise the
    spaces will be printed at the end of the previous counter value?


    Though would \r even work if this was to be run on a Mac -- I thought
    Macs used to use \r for line ending (maybe OS-X has gone to \n).


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
    Dennis Lee Bieber, Jan 15, 2004
    #9
  10. | Dennis Lee Bieber said |
    > Don't you need a \r on the second print line also? Otherwise the
    > spaces will be printed at the end of the previous counter value?


    Um, yeah. You do.

    >
    >
    > Though would \r even work if this was to be run on a Mac -- I
    > thought
    > Macs used to use \r for line ending (maybe OS-X has gone to \n).


    I don't know. I've never owned a mac, and haven't ever worked on one.
    Thus, mac nuances rarely enter my mind even when I know about them.

    Sam Walters.

    --
    Never forget the halloween documents.
    http://www.opensource.org/halloween/
    """ Where will Microsoft try to drag you today?
    Do you really want to go there?"""
    Samuel Walters, Jan 16, 2004
    #10
  11. Samuel Walters fed this fish to the penguins on Thursday 15 January
    2004 17:23 pm:


    >
    > I don't know. I've never owned a mac, and haven't ever worked on one.
    > Thus, mac nuances rarely enter my mind even when I know about them.
    >

    While I don't know about OS-X (being a BSD/Mach core?), I'm sure the
    earlier versions used <CR> (\r) for line endings (I think the TRS-80
    did too). The Amiga used <LF> (\n)... And of course, you get the
    confusion in Windows of <CR><LF> (but not <LF><CR>!)


    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Bestiaria Home Page: http://www.beastie.dm.net/ <
    > Home Page: http://www.dm.net/~wulfraed/ <
    Dennis Lee Bieber, Jan 16, 2004
    #11
  12. "Totte Karlsson" <> writes:

    > Any alternatives to ncurses? It seems like a overkill for this...

    import sys, time
    nrchars = 0

    for i in xrange(10):
    sys.stdout.write("\b \b"*nrchars)
    sys.stdout.flush()
    Str = "%i seconds to go" % (10-i)
    nrchars = len(Str)
    sys.stdout.write(Str)
    sys.stdout.flush()
    time.sleep(1)
    print


    --
    Valentino Volonghi, Regia SpA, Milan
    Linux User #310274, Gentoo Proud User
    Valentino Volonghi aka Dialtone, Jan 16, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nicholas
    Replies:
    0
    Views:
    422
    nicholas
    Dec 7, 2004
  2. Kevin
    Replies:
    1
    Views:
    9,490
    Roland
    Feb 5, 2005
  3. Totte Karlsson

    Printing to console, no scroll

    Totte Karlsson, Jan 14, 2004, in forum: Python
    Replies:
    8
    Views:
    575
    Will Stuyvesant
    Jan 16, 2004
  4. Sathiamoorthy

    Enable Scroll up and scroll down in CR-report

    Sathiamoorthy, Nov 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    869
    zobalee
    Nov 28, 2006
  5. UJ
    Replies:
    1
    Views:
    549
    John Timney \(MVP\)
    Nov 1, 2006
Loading...

Share This Page