V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
salamanderMH
V2EX  ›  问与答

Python requests 库下载文件有点问题

  •  
  •   salamanderMH · Jan 27, 2021 · 657 views
    This topic created in 1923 days ago, the information mentioned may be changed or developed.

    简单代码:

    def save_file(self, folder, url):
        if not url:
            return
    
        filename = url.rsplit("/", 1)[-1]
        path = os.path.join(folder, filename)
        if os.path.exists(path):
            return path
    
        r = requests.get(url)
        with open(path, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
        return path
    

    这个方法下载文件有时候会有问题,会下载不完整,python 也不是特别熟,看不出啥问题呢,请大佬们指教。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2536 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:04 · PVG 13:04 · LAX 22:04 · JFK 01:04
    ♥ Do have faith in what you're doing.