[Python] Python tempfile SpooledTemporaryFile write to a gzip without writing to disk where...

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a snippet of Python code that first writes a possibly large text file (think csv) to a SpooledTemporaryFile in memory. Then that spooled file is compressed with gzip. While some of the temp files may write to disk when they exceed a given size I want to expect or test that this code works in both outcomes. Given this, I have also only managed to do so where I write the gzip file in text mode? Am I missing something here?

    import tempfile
    import gzip

    words = ["one", "two", "three", "four", "five", "six", "seven", "eight"]

    with tempfile.SpooledTemporaryFile(mode='wt+') as tmp_file:
    tmp_file.write("\n".join(words))
    tmp_file.seek(0)
    with gzip.open(filename="test2.gz", mode="wt", compresslevel=7) as giz:
    giz.write(tmp_file.read())


    I have been using gzip --test --verbose and gzcat to help me validate this.

    Continue reading...

Compartilhe esta Página