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 0x1100b0a00>
/var/folders/25/cy4pgsqn2393lvfmh420gf7h0000gn/T/tmphgwptjp1
<tempfile._TemporaryFileWrapper object at 0x110124100>
/var/folders/25/cy4pgsqn2393lvfmh420gf7h0000gn/T/tmpzpczhxsc

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 0x10638aa00>
/var/folders/25/cy4pgsqn2393lvfmh420gf7h0000gn/T/tmpfileopnp8xhs.csv
/var/folders/25/cy4pgsqn2393lvfmh420gf7h0000gn/T/hello-worldftaf0luh.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=3 mode='w' encoding='utf-8'>
3