Printing to console (No Scroll)

T

Totte Karlsson

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
 
D

Diez B. Roggisch

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
 
J

Joe Francia

Totte said:
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()
 
T

Totte Karlsson

Any alternatives to ncurses? It seems like a overkill for this...
cheers
/totte
 
D

Diez B. Roggisch

Totte said:
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
 
D

Derek

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()
 
S

Samuel Walters

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.
 
D

Dennis Lee Bieber

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).


--
 
S

Samuel Walters

| 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.
 
D

Dennis Lee Bieber

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>!)


--
 
V

Valentino Volonghi aka Dialtone

Totte Karlsson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top