Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate temporary file in django and then destroy

Tags:

python

django

I am doing some file processing and for generating the file i need to generate some temporary file from existing data and then use that file as input to my function.

But i am confused where should i save that file and then delete it.

Is there any temp location where files automatically gets deleted after user session

like image 318
Mirage Avatar asked Nov 29 '12 05:11

Mirage


People also ask

How do I create a tmp file?

To create and use a temporary file The application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.

How do I delete temp files in Python?

gettempdir() to get the directory where all the temp files are stored. After running the program, if you go to temp_dir (which is /tmp in my case – Linux), you can see that the newly created file 3 is not there. This proves that Python automatically deletes these temporary files after they are closed.

How do I create a temp folder?

Open your File Explorer (it's usually the first button on your desktop taskbar, looks like a folder). Go to the "This PC" section on the left, and then double-click your C: drive. On the Home tab at the top, click "New Folder" and name it "Temp".


4 Answers

Python has the tempfile module for exactly this purpose. You do not need to worry about the location/deletion of the file, it works on all supported platforms.

There are three types of temporary files:

  • tempfile.TemporaryFile - just basic temporary file,
  • tempfile.NamedTemporaryFile - "This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name attribute of the file object.",
  • tempfile.SpooledTemporaryFile - "This function operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().",

EDIT: The example usage you asked for could look like this:

>>> with TemporaryFile() as f:         f.write('abcdefg')         f.seek(0)  # go back to the beginning of the file         print(f.read())       abcdefg 
like image 80
Tadeck Avatar answered Sep 28 '22 03:09

Tadeck


You should use something from the tempfile module. I think that it has everything you need.

like image 42
mgilson Avatar answered Sep 28 '22 02:09

mgilson


I would add that Django has a built-in NamedTemporaryFile functionality in django.core.files.temp which is recommended for Windows users over using the tempfile module. This is because the Django version utilizes the O_TEMPORARY flag in Windows which prevents the file from being re-opened without the same flag being provided as explained in the code base here.

Using this would look something like:

from django.core.files.temp import NamedTemporaryFile

temp_file = NamedTemporaryFile(delete=True)

Here is a nice little tutorial about it and working with in-memory files, credit to Mayank Jain.

like image 26
Samwise Ganges Avatar answered Sep 28 '22 02:09

Samwise Ganges


I just added some important changes: convert str to bytes and a command call to show how external programs can access the file when a path is given.

import os
from tempfile import NamedTemporaryFile
from subprocess import call

with NamedTemporaryFile(mode='w+b') as temp:
    # Encode your text in order to write bytes
    temp.write('abcdefg'.encode())
    # put file buffer to offset=0
    temp.seek(0)

    # use the temp file
    cmd = "cat "+ str(temp.name)
    print(os.system(cmd))
like image 28
alemol Avatar answered Sep 28 '22 04:09

alemol