Quiet output from write(chunk)

前端 未结 1 648
既然无缘
既然无缘 2021-01-28 17:25

Working in the Python interpreter, the following code works ... but with a console output of: 2048 2048 ... 2048 2048

Is there anyway to run the fd.write(chunk) statemen

相关标签:
1条回答
  • 2021-01-28 18:09

    The Python interpreter prints the return value of functions by default. Write returns the number of bytes it writes. If you don't want to see that, just store it in a variable which you never print, e.g.:

    r = requests.get(url, stream=True)
    with open('output.jpg', 'wb') as fd:
        for chunk in r.iter_content(2048):
            bytes = fd.write(chunk)
    
    0 讨论(0)
提交回复
热议问题