How can I create a tmp file in Python?

I think you’re looking for a tempfile.NamedTemporaryFile.

import tempfile
with tempfile.NamedTemporaryFile() as tmp:
    print(tmp.name)
    tmp.write(...)

But:

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).

If that is a concern for you:

import os, tempfile
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
    print(tmp.name)
    tmp.write(...)
finally:
    tmp.close()
    os.unlink(tmp.name)

Leave a Comment

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