Best way to create a temporary directory on AWS Lambda Function with Python?

Question:

Possibly being a bit knit-picky, but does anyone know of a more "elegant" method for doing this?

Right now I create tmp directories like this:

tmp_file_path_obj = Path(mktemp(dir=f"/tmp/path/to/nested/dir/with/non/existent/parents"))
tmp_file_path_obj.parent.mkdir(exist_ok=True, parents=True)

I would like a method that takes advantage of something like mkdtemp but when I try that I get FileNotFound errors since the parent directories don’t exist yet and I’m not able to pass in the exist_ok or parents parameter.

I guess I’m looking for some equivalent to `mkdtemp(dir="path/to/dir", exist_ok=True, parents=True)

Any ideas?

Thanks!

Asked By: perry_the_python

||

Answers:

import tempfile

temp_dir = tempfile.TemporaryDirectory()

Answered By: ANISH KUMAR