Python temporary file

Python temporary file

August 2, 2020 | python, programming, libs

tags
Python Programming

Python’s tempfile standard library is pretty neat when we need to create a temporary file and/or directories. Instead of having a much of code like this:

import pathlib

def create_file(filename):
    if pathlib.Path(filename).exists():
        # handle path removal/rewrite/backup
        pass
    with open(filename, "w") as f:
        # write to file
        pass

We can ask Python to take care of handling this type of tasks for us. tempfile handles most of the OS API, so we can focus on writting the logic instead.

import tempfile

def create_file():
    return tempfile.NamedTemporaryFile(mode="w", encoding="utf-8")

# file one
print(create_file())
print(create_file().name)

# file two
print(create_file())
print(create_file().name)
<tempfile._TemporaryFileWrapper object at 0x10d173d30>
/var/folders/gf/7vcrg3d57pn8j_30zvkz94x00000gn/T/tmphxf3mze8
<tempfile._TemporaryFileWrapper object at 0x10d1b1130>
/var/folders/gf/7vcrg3d57pn8j_30zvkz94x00000gn/T/tmpsz20r_u3

NamedTemporaryFile returns a file-like object that can be used as a temporary storage, however, contrary to TemporaryFile, a file created with NamedTemporaryFile is guaranteed to have a visible name during its lifetime. TemporaryFile gets destroyed as soon as the file is closed, NamedTemporaryFile has support for the delete flags, which defaults to True

You can also change the default filename prefix and suffix.

import tempfile

def create_file(suffix=".json", prefix="tmpfile"):
    return tempfile.NamedTemporaryFile(
        mode="w",
        encoding="utf-8",
        suffix=suffix,
        prefix=prefix,
    )

# file one

print(create_file(suffix=".csv"))
print(create_file(suffix=".csv").name)
print(create_file(suffix=".csv", prefix="hello-world").name)
<tempfile._TemporaryFileWrapper object at 0x10df3d0a0>
/var/folders/gf/7vcrg3d57pn8j_30zvkz94x00000gn/T/tmpfilei92azs5a.csv
/var/folders/gf/7vcrg3d57pn8j_30zvkz94x00000gn/T/hello-worlduk8_07f5.csv

With TemporaryFile it returns a TextIOWrapper:

import tempfile

def create_file(suffix=".json", prefix="tmpfile"):
    return tempfile.TemporaryFile(
        mode="w",
        encoding="utf-8",
        suffix=suffix,
        prefix=prefix,
    )

# file one
print(create_file(suffix=".csv"))
print(create_file(suffix=".csv", prefix="hello-world").name)
<_io.TextIOWrapper name=6 mode='w' encoding='utf-8'>
6


Go to random page