tempfile

Table of Contents

Create a temporary file howto

from tempfile import TemporaryFile

with TemporaryFile() as f:
    f.write('test')
    f.seek(0)
    # Pass f to wherever accepts a file object

If you need a path for downloading or something, use NamedTemporaryFile, and access the path with name attribute:

from tempfile import NamedTemporaryFile

with NamedTemporaryFile() as f:
    print(f.name)
/var/folders/f8/5g36khdd3_v0_294fjltlw7w0000gn/T/tmpz3y8z07y