Permission Denied To Write To My Temporary File

NamedTemporaryFile actually creates and opens the file for you, there’s no need for you to open it again for writing.

In fact, the Python docs state:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

That’s why you’re getting your permission error. What you’re probably after is something like:

f = tempfile.NamedTemporaryFile(mode="w") # open file
temp = f.name                             # get name (if needed)

Leave a Comment