How to use tempfile.NamedTemporaryFile() in Python

This could be one of two reasons:

Firstly, by default the temporary file is deleted as soon as it is closed. To fix this use:

tf = tempfile.NamedTemporaryFile(delete=False)

and then delete the file manually once you’ve finished viewing it in the other application.

Alternatively, it could be that because the file is still open in Python Windows won’t let you open it using another application.

Edit: to answer some questions from the comments:

As of the docs from 2 when using delete=False the file can be removed by using:

 tf.close()
 os.unlink(tf.name)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)