Python Script: Print new line each time to shell r

2019-05-14 00:09发布

I am a noob when it comes to python. I have a python script which gives me output like this:

[last] ZVZX-W3vo9I: Downloading video webpage
[last] ZVZX-W3vo9I: Extracting video information
[download] Destination: myvideo.flv
[download]   9.9% of 10.09M at    3.30M/s ETA 00:02

The last line keeps getting updated with new values of progress. I want to change this. Instead of updating I want a new line to be printed each time. How can i do this? I think the part concerned is this bit:

def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
    """Report download progress."""
    self.to_stdout(u'\r[download] %s of %s at %s ETA %s' %
        (percent_str, data_len_str, speed_str, eta_str), skip_eol=True)

If more code needs to be seen please let me know so that I can show you what is needed to solve this.

Thank you very much for any help.

标签: python shell
4条回答
冷血范
2楼-- · 2019-05-14 00:41

I'm thinking you may just need to change:

skip_eol=True

to:

skip_eol=False

and get rid of the "\r" to see what happens. I think you'll be pleasantly surprised :-)

查看更多
叛逆
3楼-- · 2019-05-14 00:51

You can take out the \r, which moves to the cursor back to the beginning of the line and take out the skip_eol=True probably. Perhaps:

   self.to_stdout(u'[download] %s of %s at %s ETA %s' %
        (percent_str, data_len_str, speed_str, eta_str))
查看更多
啃猪蹄的小仙女
4楼-- · 2019-05-14 00:58

If I understand your request properly, you should be able to change that function to this:

def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
    """Report download progress."""
    print u'[download] %s of %s at %s ETA %s' % (percent_str, data_len_str, speed_str, eta_str)

That will print the output on a new line each time.

查看更多
时光不老,我们不散
5楼-- · 2019-05-14 01:03

The "update" effect is achieved by '\r'.

Try this in a Python (2.x) shell:

print "00000000\r1111"

\r just returns the cursor to the beginning of the line.

查看更多
登录 后发表回答