在Python 3中逐部分下载文件

发布于 2024-12-12 21:32:32 字数 309 浏览 2 评论 0原文

我正在使用 Python 3 下载文件:

local_file = open(file_name, "w" + file_mode)
local_file.write(f.read())
local_file.close()

此代码可以工作,但它首先将整个文件复制到内存中。这是非常大的文件的问题,因为我的程序会占用内存。 (对于一个200 MB的文件,从17M内存到240M内存)

我想知道Python中是否有一种方法可以下载文件的一小部分(数据包),将其写入文件,从内存中删除它,并保留重复该过程,直到文件完全下载。

I'm using Python 3 to download a file:

local_file = open(file_name, "w" + file_mode)
local_file.write(f.read())
local_file.close()

This code works, but it copies the whole file into memory first. This is a problem with very big files because my program becomes memory hungry. (Going from 17M memory to 240M memory for a 200 MB file)

I would like to know if there is a way in Python to download a small part of a file (packet), write it to file, erase it from memory, and keep repeating the process until the file is completely downloaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-12-19 21:32:33

尝试使用此处描述的方法:

在Python中读取大文件的惰性方法?

我特别指的是已接受的答案。让我也将其复制到此处,以确保回复完全清晰。

    def read_in_chunks(file_object, chunk_size=1024):
        """Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1k."""
        while True:
            data = file_object.read(chunk_size)
            if not data:
                break
            yield data


    f = open('really_big_file.dat')
    for piece in read_in_chunks(f):
        process_data(piece)

这可能会适应您的需求:它以较小的块读取文件,允许在不填满整个内存的情况下进行处理。如果您还有任何疑问,请回来。

Try using the method described here:

Lazy Method for Reading Big File in Python?

I am specifically referring to the accepted answer. Let me also copy it here to ensure complete clarity of response.

    def read_in_chunks(file_object, chunk_size=1024):
        """Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1k."""
        while True:
            data = file_object.read(chunk_size)
            if not data:
                break
            yield data


    f = open('really_big_file.dat')
    for piece in read_in_chunks(f):
        process_data(piece)

This will likely be adaptable to your needs: it reads the file in smaller chunks, allowing for processing without filling your entire memory. Come back if you have any further questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文