Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'subject:skip:s 10': 0.05; 'sys': 0.05; 'append': 0.07; 'line:': 0.07; 'seemed': 0.07; 'newly': 0.09; 'overwrite': 0.09; 'snippet': 0.09; 'python': 0.11; 'properly': 0.15; '>on': 0.16; 'breeding': 0.16; 'carriage': 0.16; 'curses': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'ibm.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'sense,': 0.16; 'sequence,': 0.16; 'simplified': 0.16; 'simpson': 0.16; 'stdlib.': 0.16; 'subject:Query': 0.16; 'wrote:': 0.16; 'say,': 0.18; 'string,': 0.18; 'to:name:python-list@python.org': 0.20; 'trying': 0.22; 'saying': 0.22; 'am,': 0.23; '2015': 0.23; 'module': 0.23; 'cheers,': 0.24; 'import': 0.24; 'seems': 0.24; 'header:In-Reply- To:1': 0.24; 'previously': 0.24; 'words': 0.24; 'mon,': 0.24; 'example': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'ibm': 0.27; 'ansi': 0.29; 'skip:2 30': 0.29; 'spaces': 0.29; 'query': 0.31; 'print': 0.31; 'common': 0.33; 'consult': 0.33; 'shorter': 0.33; 'subject:regarding': 0.33; 'previous': 0.34; 'to:addr:python-list': 0.35; 'clear': 0.35; 'robert': 0.35; 'text.': 0.35; "isn't": 0.35; 'skip:o 20': 0.35; 'but': 0.36; 'being': 0.36; 'there': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'version': 0.38; 'is,': 0.38; 'end': 0.39; 'whatever': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'some': 0.40; 'term': 0.60; 'content-disposition:inline': 0.60; 'your': 0.60; 'times': 0.61; 'details': 0.63; 'making': 0.64; 'cameron': 0.66; 'capabilities': 0.72; 'received:61': 0.72; 'nouns,': 0.84; 'skip:/ 30': 0.84; 'technique.': 0.84 X-Authentication-Info: Submitted using ID cskk@bigpond.com X-Authority-Analysis: v=2.0 cv=Os7NOlDt c=1 sm=1 a=ly0juX3RbyrIRiZ3+OORKg==:17 a=yEdEr6MRgwAA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=XAFQembCKUMA:10 a=pGLkceISAAAA:8 a=zr__qXfqAAAA:8 a=sRatYPzCEuESRbCVr4cA:9 a=CjuIK1q_8ugA:10 a=ly0juX3RbyrIRiZ3+OORKg==:117 Date: Mon, 8 Jun 2015 11:56:25 +1000 From: Cameron Simpson To: "python-list@python.org" Subject: Re: Query regarding sys.stdout.write MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) References: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433732566 news.xs4all.nl 2918 [2001:888:2000:d::a6]:60057 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92324 On 08Jun2015 10:18, Chris Angelico wrote: >On Mon, Jun 8, 2015 at 7:17 AM, Sreenath Nair 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: If you want do do this version portably, hook into the Python curses module and get the result of curses.tigetstr('el'). If that returns None, the terminal does not support clear-to-end-of-line and you should use the spaces-overwrite technique. But if it returns a string, you can write that string, whatever it is, to erase to the end of the line. Most modern terminals are ANSI supersets, but the terminfo database has a huge suite of descriptions of terminals, and this will get your the right sequence, whatever that may be, for your current terminal. Of course, reaching for the curses module is massive overkill in a sense, but it is the only way to consult terminfo in the Python stdlib. See "man terminfo" for details on what terminal capabilities the terminfo database describes. Cheers, Cameron Simpson But I have to say, I "non-concur." (Non-concur was a term I learned at IBM. IBM seemed to be a breeding ground for making up words and phrases, turning verbs into nouns, etc. "Non-concur" seems to be for those times when saying "I disagree" just isn't strong enough!) - Robert D. Seidman