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


Groups > comp.lang.python > #92316

Re: Query regarding sys.stdout.write

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:skip:s 10': 0.05; 'sys': 0.05; 'append': 0.07; 'line:': 0.07; 'newly': 0.09; 'overwrite': 0.09; 'snippet': 0.09; 'url:github': 0.09; 'cc:addr :python-list': 0.10; 'systems.': 0.11; 'properly': 0.15; 'carriage': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'length,': 0.16; 'simplified': 0.16; 'subject:Query': 0.16; 'wider': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'trying': 0.22; 'am,': 0.23; '2015': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'previously': 0.24; 'mon,': 0.24; 'example': 0.25; 'message- id:@mail.gmail.com': 0.28; 'skip:2 30': 0.29; 'spaces': 0.29; 'guess': 0.29; '(which': 0.29; 'lines': 0.30; 'query': 0.31; 'window': 0.31; 'print': 0.31; 'common': 0.33; 'retain': 0.33; 'shorter': 0.33; 'subject:regarding': 0.33; 'case,': 0.34; 'previous': 0.34; 'received:google.com': 0.34; 'needed': 0.34; 'clear': 0.35; 'text.': 0.35; 'skip:o 20': 0.35; 'but': 0.36; 'being': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'end': 0.39; 'some': 0.40; 'your': 0.60; 'back': 0.61; 'cameron': 0.66; 'fall': 0.66; 'chrisa': 0.84; 'me;': 0.84; 'skip:/ 30': 0.84; 'try,': 0.84; 'url:master': 0.84; 'to:none': 0.90
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=bMXDxxRZL1z6zoCUxvV7/r6pI/FvKZ29C4a1giGtXuA=; b=HaxI30CaeZa5hWvQb6zLeNA4Kr3suVh/XioRrw5NGDfBs6mySCdK+zepsSWg7ZtR7K DQtd7RniqAzG7JrgrQQf8oqvtarTxTRmDJRy6oYixHUNoXQWi06k0svov8fW4s1B0L7s qS0BwmbqGO+MofMSJ9KdoLyxoyY0ieIHbO1rI8SH2yeS27CfQRVa7FiD+49QXiwRv3xm z9iajWoRPWBegscVtVPLbNvlkCnMLM2NRCbSu1oRXvUv+iwE/OuJw3wiU0Nkz81SEdt0 C2CT8MUy+a2Dky8NXBYRWpTwml5rNkZGVXMFyR4voFbAfGJi5Ip/VcHGoFHnjJdXnASF c1HQ==
MIME-Version 1.0
X-Received by 10.50.61.241 with SMTP id t17mr10352679igr.34.1433722697095; Sun, 07 Jun 2015 17:18:17 -0700 (PDT)
In-Reply-To <BLU402-EAS68B18DD7A88AF142BC67B7E3B00@phx.gbl>
References <BLU402-EAS68B18DD7A88AF142BC67B7E3B00@phx.gbl>
Date Mon, 8 Jun 2015 10:18:17 +1000
Subject Re: Query regarding sys.stdout.write
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.283.1433722699.13271.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433722699 news.xs4all.nl 2825 [2001:888:2000:d::a6]:54072
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 5193
X-Received-Body-CRC 2033402241
Xref csiph.com comp.lang.python:92316

Show key headers only | View raw


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


Thread

Re: Query regarding sys.stdout.write Chris Angelico <rosuav@gmail.com> - 2015-06-08 10:18 +1000

csiph-web