Permission Denied To Write To My Temporary File

I am attempting to create and write to a temporary file on Windows OS using Python. I have used the Python module tempfile to create a temporary file.

But when I go to write that temporary file I get an error Permission Denied. Am I not allowed to write to temporary files?! Am I doing something wrong? If I want to create and write to a temporary file how should should I do it in Python? I want to create a temporary file in the temp directory for security purposes and not locally (in the dir the .exe is executing).

IOError: [Errno 13] Permission denied: 'c:\\users\\blah~1\\appdata\\local\\temp\\tmpiwz8qw'

temp = tempfile.NamedTemporaryFile().name
f = open(temp, 'w') # error occurs on this line

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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 =                             # get name (if needed)

Solution 2

Use the delete parameter as below:

tmpf = NamedTemporaryFile(delete=False)

But then you need to manually delete the temporary file once you are done with it.


Reference for bug:


Solution 3

Consider using os.path.join(tempfile.gettempdir(), os.urandom(24).hex()) instead. It’s reliable, cross-platform, and the only caveat is that it doesn’t work on FAT partitions.

NamedTemporaryFile has a number of issues, not the least of which is that it can fail to create files because of a permission error, fail to detect the permission error, and then loop millions of times, hanging your program and your filesystem.

Solution 4

The following custom implementation of named temporary file is expanded on the original answer by Erik Aronesty:

import os
import tempfile

class CustomNamedTemporaryFile:
    This custom implementation is needed because of the following limitation of tempfile.NamedTemporaryFile:

    > 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).
    def __init__(self, mode='wb', delete=True):
        self._mode = mode
        self._delete = delete

    def __enter__(self):
        # Generate a random temporary file name
        file_name = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
        # Ensure the file is created
        open(file_name, "x").close()
        # Open the file in the given mode
        self._tempFile = open(file_name, self._mode)
        return self._tempFile

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self._delete:

Solution 5

This issue might be more complex than many of you think. Anyway this was my solution:

  1. Make use of atexit module
def delete_files(files):
    for file in files:
  1. Make NamedTemporaryFile delete=False
temp_files = []
result_file = NamedTemporaryFile(dir=tmp_path(), suffix=".xlsx", delete=False)
  1. Register delete_files as a clean up function
atexit.register(delete_files, temp_files)

Solution 6

Permission was denied because the file is Open during line 2 of your code.

close it with f.close() first then you can start writing on your tempfile

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply