Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 310
Mack Avatar asked Apr 22 '14 06:04

Mack


People also ask

How do I fix unable to create a temporary file?

Creating an executable file requires creation of temporary files. This error has the following cause and solution: The drive that contains the directory specified by the TEMP environment variable is full. Delete files from the full drive or specify a different drive in the TEMP environment variable.

How do I get permission to denied a file?

Just do an "chmod +x log" to fix that perm and you should be able to access it.


2 Answers

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) 
like image 168
paxdiablo Avatar answered Oct 18 '22 01:10

paxdiablo


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.

tmpf.close() os.unlink(tmpf.name) 

Reference for bug: https://github.com/bravoserver/bravo/issues/111

regards, Vidyesh

like image 42
Vidyesh Ranade Avatar answered Oct 18 '22 02:10

Vidyesh Ranade