Why does the TemporaryUploadedFile
get deleted in Python 3 while in Python 2 it stays in /tmp/
directory? And how would I keep the behavior of Python 2 while using 3?
from django.core.files.uploadedfile import TemporaryUploadedFile
with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file:
tmp_file_path = tmp_file.temporary_file_path()
tmp_file.file.delete = False
print(tmp_file_path)
Running this block of code under Python 2 keeps the file in /tmp/
directory while on Python 3 it gets deleted.
[ray@fedora tmp]$ ls | grep tmp
tmpvSmI8b.upload #generated in Python 2
PY2 version 2.7.18
PY3 version 3.7.12
Django 1.11.29
Don't know the details of why this behavior is different but if you want to keep file in /tmp/
you need to instantiate NamedTemporaryFile
with delete=False
atrributes. Like this:
NamedTemporaryFile(delete=False, ...)