Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92316
| References | <BLU402-EAS68B18DD7A88AF142BC67B7E3B00@phx.gbl> |
|---|---|
| Date | 2015-06-08 10:18 +1000 |
| Subject | Re: Query regarding sys.stdout.write |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.283.1433722699.13271.python-list@python.org> (permalink) |
On Mon, Jun 8, 2015 at 7:17 AM, 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?
The most common solution is to print out some spaces to overwrite the
previous text. It's simple, straight-forward, and works on all
systems. But if your console is properly ANSI-compliant, you may be
able to simply append \33[K to clear to end of line:
https://github.com/Rosuav/LetMeKnow/blob/master/letmeknow.py#L172
No need to retain the previous line length, no need to guess at the
length required, and no lengthening the long lines (which annoyed me;
I had to make the console window X characters wider than it needed to
be, else the spaces would make it wrap). Give it a try, see what
happens; worst case, fall back on what Cameron suggested.
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Query regarding sys.stdout.write Chris Angelico <rosuav@gmail.com> - 2015-06-08 10:18 +1000
csiph-web