Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python将文件直接下载到文件系统_Python_Python 3.x_Get_Wget - Fatal编程技术网

用Python将文件直接下载到文件系统

用Python将文件直接下载到文件系统,python,python-3.x,get,wget,Python,Python 3.x,Get,Wget,有没有办法将文件直接流式传输到文件系统?即使连接丢失,我也希望在fs中看到该特定文件中的所有内容。就像wget或curl一样 但是,使用request会导致一个问题,即首先下载响应的内容,然后将其写入文件系统 with open(file_name, "wb") as file: response = get(url) # may take some time file.write(response.content) 问题:当文件“下载”时,它存储在其他地方(我猜是在内存中,或

有没有办法将文件直接流式传输到文件系统?即使连接丢失,我也希望在fs中看到该特定文件中的所有内容。就像wget或curl一样

但是,使用request会导致一个问题,即首先下载响应的内容,然后将其写入文件系统

with open(file_name, "wb") as file:
    response = get(url) # may take some time
    file.write(response.content)
问题:当文件“下载”时,它存储在其他地方(我猜是在内存中,或者是文件系统中的一个临时splace)。这意味着只要请求没有(成功)完成,我就有一个0字节的文件


我是否可以在不使用第三方库的情况下解决此问题?

可以通过
请求
stream=true
实现直接流式传输到文件,或者请参见

with open(file_name, 'wb') as f:
    with requests.get(url, stream=True) as r:
        shutil.copyfileobj(r.raw, f)