2008年11月21日金曜日

python urllib.urlretrieve で進捗表示しつつダウンロードのメモ


import sys
import urllib

def _reporthook(blocknum, bs, size):
sys.stdout.write("%4d%%\r"%(blocknum*bs*100/size))
sys.stdout.flush()

if __name__ == "__main__":
uri = sys.argv[1]
dest = uri[uri.rfind('/')+1:]
urllib.urlretrieve(uri, dest, _reporthook)
print

参考: http://subtech.g.hatena.ne.jp/cho45/20081120/1227178806
http://blog.livedoor.jp/dankogai/archives/51141631.html
printfやputsはflushいらんの?
pythonはprintでもダメだった
よくわからない