如何在django中生成临时文件然后销毁

时间:2021-10-01 15:56:47

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

用户会话后是否有文件自动删除的临时位置

2 个解决方案

#1


39  

Python has 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.

Python有tempfile模块用于此目的。您无需担心文件的位置/删除,它适用于所有支持的平台。

There are three types of temporary files:

临时文件有三种类型:

  • tempfile.TemporaryFile - just basic temporary file,
  • tempfile.TemporaryFile - 只是基本的临时文件,
  • 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.NamedTemporaryFile - “此函数与TemporaryFile()完全相同,但保证文件在文件系统中具有可见名称(在Unix上,目录条目未取消链接)。可以从名称中检索该名称文件对象的属性。“,
  • 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().",
  • tempfile.SpooledTemporaryFile - “这个函数与TemporaryFile()完全一样,除了数据在内存中假脱机,直到文件大小超过max_size,或者直到文件的fileno()方法被调用,此时内容被写入磁盘并且操作与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

#2


0  

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

你应该使用tempfile模块中的东西。我认为它拥有你需要的一切。

#1


39  

Python has 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.

Python有tempfile模块用于此目的。您无需担心文件的位置/删除,它适用于所有支持的平台。

There are three types of temporary files:

临时文件有三种类型:

  • tempfile.TemporaryFile - just basic temporary file,
  • tempfile.TemporaryFile - 只是基本的临时文件,
  • 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.NamedTemporaryFile - “此函数与TemporaryFile()完全相同,但保证文件在文件系统中具有可见名称(在Unix上,目录条目未取消链接)。可以从名称中检索该名称文件对象的属性。“,
  • 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().",
  • tempfile.SpooledTemporaryFile - “这个函数与TemporaryFile()完全一样,除了数据在内存中假脱机,直到文件大小超过max_size,或者直到文件的fileno()方法被调用,此时内容被写入磁盘并且操作与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

#2


0  

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

你应该使用tempfile模块中的东西。我认为它拥有你需要的一切。