Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92310 > unrolled thread

Re: Query regarding sys.stdout.write

Started byCameron Simpson <cs@zip.com.au>
First post2015-06-08 08:40 +1000
Last post2015-06-08 08:40 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Query regarding sys.stdout.write Cameron Simpson <cs@zip.com.au> - 2015-06-08 08:40 +1000

#92310 — Re: Query regarding sys.stdout.write

FromCameron Simpson <cs@zip.com.au>
Date2015-06-08 08:40 +1000
SubjectRe: Query regarding sys.stdout.write
Message-ID<mailman.279.1433716866.13271.python-list@python.org>
On 08Jun2015 02:47, Sreenath Nair <sreenath.cg@gmail.com> wrote:
>I have a general query about the following snippet:
>
>import os
>Import sys
>for each_dir in os.listdir("/home/tmpuser"):
>    full_path = os.path.join("/home/tmpuser", each_dir)
>    sys.stdout.write("\r%s" % full_path)
>    sys.stdout.flush()
>
>The snippet is a simplified example of me trying to print to the same line by using carriage return. This is working fine. However, the issue is that if the previous line was longer than the current line being printed then there are characters leftover from the previous print. Like so:
>
>Print no. 1: /home/tmpuser/somedir/somefile.ext
>Print no. 2:/home/tmpuser/somefile.extmefile.ext
>
>In case of the newly printed shorter line, the characters from the previously printed longer line are leftover... Is there any way to clear the previous print? While still being able to print to the same line?

Certainly. Keep track of the length of the previous line. Pad the new line with 
sufficient spaces to overwrite the old line, and then sufficient backspaces to 
get back to the end of the new line.

For added points, write a minimal updated that notices if the old line had a 
common prefix with the new line, and emit only the change.

Cheers,
Cameron Simpson <cs@zip.com.au>

The worst tyrannies were the ones where a governance required its own logic on
every embedded node. - Vernor Vinge

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web