Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder03.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-10.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Query regarding sys.stdout.write Organization: Decebal Computing References: X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Mon, 08 Jun 2015 00:30:49 +0200 Message-ID: <874mmjnobq.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:SUHsBWk/KMma0+ceb9Txmr+uHGo= MIME-Version: 1.0 Content-Type: text/plain Lines: 33 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1433717152 news.kpn.nl 20709 81.207.62.244@kpn/81.207.62.244:34821 Xref: csiph.com comp.lang.python:92313 On Sunday 7 Jun 2015 23:17 CEST, 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? Say that you know that a line will never be longer as 80 characters, then you could do something like: sys.stdout.write('\r{0:80}'.format(full_path)) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof