Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1579
囚心锁ツ
囚心锁ツ 2020-12-02 04:42

I have managed to get my first python script to work which downloads a list of .ZIP files from a URL and then proceeds to extract the ZIP files and writes them to disk.

9条回答
  •  星月不相逢
    2020-12-02 05:15

    write to a temporary file which resides in RAM

    it turns out the tempfile module ( http://docs.python.org/library/tempfile.html ) has just the thing:

    tempfile.SpooledTemporaryFile([max_size=0[, mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]]])

    This function operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().

    The resulting file has one additional method, rollover(), which causes the file to roll over to an on-disk file regardless of its size.

    The returned object is a file-like object whose _file attribute is either a StringIO object or a true file object, depending on whether rollover() has been called. This file-like object can be used in a with statement, just like a normal file.

    New in version 2.6.

    or if you're lazy and you have a tmpfs-mounted /tmp on Linux, you can just make a file there, but you have to delete it yourself and deal with naming

提交回复
热议问题