Printing digits in one place

O

Oltmans

I'm writing a file-transfer program and I'm receiving bytes
transferred in a function. Now I would like to print bytes transfered
in one place e.g.

Bytes Transfered so far X

and X will increase and cursor should stay at this position. I don't
want to use any 3rd party module for this. Can I somehow do that
without using any third-party module?

I've been able to do this with Console module available at
http://effbot.org/zone/console-handbook.htm but I would want to do
this without the Console module. Actually , I'm able to initialize the
Console module and print the bytes transferrred information but I
can't find a way to exit from Console module so that my other
functions can proceed with normal display using 'print' statement.

Any ideas will be appreciated.
 
S

Steve Holden

Oltmans said:
I'm writing a file-transfer program and I'm receiving bytes
transferred in a function. Now I would like to print bytes transfered
in one place e.g.

Bytes Transfered so far X

and X will increase and cursor should stay at this position. I don't
want to use any 3rd party module for this. Can I somehow do that
without using any third-party module?

I've been able to do this with Console module available at
http://effbot.org/zone/console-handbook.htm but I would want to do
this without the Console module. Actually , I'm able to initialize the
Console module and print the bytes transferrred information but I
can't find a way to exit from Console module so that my other
functions can proceed with normal display using 'print' statement.

Any ideas will be appreciated.

Try repeatedly writing something like

"\r \rBytes Transferred So Far %d" \
% count

Not the fastest approach, but probably fast enough.

If you *must* have the cursor on the number of bytes transferred
then try using backspaces after you have printed out the number. But
you'll need to track how long the number is to put out the right number
of backspaces. Something like (untested)

sys.stdout.write("Bytes Transferred So Far")
for ...
...
cs = str(count)
csl = len(cs)
sys.stdout.write("%s%s%s%s" % (
" "*csl, "\b"*csl, cs, "\b"*csl)

Use sys.stdout.write() for full control over spacing and newlines.

regards
Steve
 
J

John Machin

I'm writing a file-transfer program and I'm receiving bytes
transferred in a function. Now I would like to print bytes transfered
in one place e.g.

Bytes Transfered so far X

and X will increase and cursor should stay at this position. I don't
want to use any 3rd party module for this. Can I somehow do that
without using any third-party module?

I've been able to do this with Console module available athttp://effbot.org/zone/console-handbook.htmbut I would want to do
this without the Console module. Actually , I'm able to initialize the
Console module and print the bytes transferrred information but I
can't find a way to exit from Console module so that my other
functions can proceed with normal display using 'print' statement.

Any ideas will be appreciated.

Try using backspaces e.g. something like this:

| >>> import time, sys
| >>> msg = ''
| >>> for i in range(15):
| ... time.sleep(2.0)
| ... nbs = len(msg)
| ... msg = str(i)
| ... sys.stdout.write('\b' * nbs + msg)
| ...

HTH,
John
 
T

Tim Chase

Try repeatedly writing something like
"\r \rBytes Transferred So Far %d" \
% count

Rather than backing up twice with \r, I'd just suggest

"\rBytes Transferred So Far %d " % count

or even

"\rBytes Transferred So Far %d%s" % (count, ' '*20)

-tkc
 
L

Lawrence D'Oliveiro

Oltmans said:
Bytes Transfered so far X

and X will increase and cursor should stay at this position.

Most terminal emulators are VT100-compatible these days. You could use something like <ESC>[80D to move back to the beginning of the same line.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top