简体   繁体   English

如何使用tempfile.NamedTemporaryFile()?

[英]How to use tempfile.NamedTemporaryFile()?

I'm working on a Python script that needs to create about 50 distinct temporary files, which are all appended frequently during the course of the script and merged at the end. 我正在开发一个Python脚本,需要创建大约50个不同的临时文件,这些文件在脚本过程中经常被附加并在最后合并。 I'm sure that the tempfile module can do what I need, but I haven't been able to figure out how from reading the documentation. 我确信tempfile模块可以做我需要的,但是我无法弄清楚如何阅读文档。

I want to use temporary files--as opposed to variables--to conserve system memory, as these data chunks grow large as the script processes tens of thousands of other files. 我想使用临时文件(而不是变量)来节省系统内存,因为这些数据块会随着脚本处理数万个其他文件而变大。

The following chunk of code is the hack I'm currently using to create these files (untemporarily) in an untemporary directory: 下面的代码块是我正在用于在非临时目录中创建这些文件(非暂时)的hack:

item = (string from another file)   # string must id file for future use
tmpfile = 'tmpfiles/' + item
if item not in totalitems:
   totalitems.add(item)
   with open(tmpfile, 'w') as itemfile:
      output = some stuff
      tmpfile.write(output)
else:
   with open(tmpfile, 'a') as itemfile:
      output = different stuff
      tmpfile.write(output)

I think what I need is tempfile.NamedTemporaryFile() . 我想我需要的是tempfile.NamedTemporaryFile() According to the documentation : 根据文件

That name can be retrieved from the name member of the file object. 可以从文件对象的名称成员中检索该名称。

Unfortunately, I don't understand what that means. 不幸的是,我不明白这意味着什么。 I just need to be able to call each file again later when I run across its corresponding "item" again in the files I'm processing. 当我在我正在处理的文件中再次运行其相应的“项目”时,我只需要能够稍后再次调用每个文件。 I presume this is rather straight forward and I'm just being dense. 我认为这是相当直接的,我只是在密集。 In case it matters, I have versions of this script for both Python 2.7.1 and 3.2.3. 如果它很重要,我有Python 2.7.1和3.2.3的这个脚本的版本。 I only really need for one or the other to work; 我真的只需要一个人或另一个人去工作; I created both just as a learning exercise. 我创造了两个作为学习练习。

"That name can be retrieved from the name member of the file object." “可以从文件对象的名称成员中检索该名称。”

means that you can get the name of the temporary file created like so: 意味着您可以获取创建的临时文件的名称,如下所示:

In [4]: import tempfile

In [5]: tf = tempfile.NamedTemporaryFile()  
In [6]: tf.name  # retrieve the name of the temp file just created
Out[6]: 'c:\\blabla\\locals~1\\temp\\tmptecp3i'

Note : By default the file will be deleted when it is closed. 注意 :默认情况下,文件将在关闭时删除。 However, if the delete parameter is False, the file is not automatically deleted. 但是,如果delete参数为False,则不会自动删除该文件。 See the Python docs on this for more information. 有关更多信息,请参阅有关此文档Python文档

Since you can retrieve the name of each of the 50 temp files you want to create, you can save them, eg, in a list, before you use them again later (as you say). 由于您可以检索要创建的50个临时文件中的每个文件的名称,因此可以在以后再次使用它们之前将它们保存在列表中(如您所说)。 Just be sure to set the delete value accordingly so that the files don't disappear when you close them (in case you plan to close, and then later reopen them). 只需确保相应地设置delete值,以便文件在关闭时不会消失(如果您打算关闭,稍后再重新打开它们)。

I explained how to create temporary filenames in more detail here Best way to generate random file names in Python 我在这里更详细地解释了如何创建临时文件名这里是在Python中生成随机文件名的最佳方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 tempfile.NamedTemporaryFile() 保存图像? - How to use tempfile.NamedTemporaryFile() to save an image? 如何在python中使用tempfile.NamedTemporaryFile() - how to use tempfile.NamedTemporaryFile() in python 如何打开(两次)使用tempfile.NamedTemporaryFile()创建的文件 - How do I open (twice) file created with tempfile.NamedTemporaryFile() 任务计划程序中的tempfile.NamedTemporaryFile崩溃 - tempfile.NamedTemporaryFile crashing in Task Scheduler 如何阻止 tempfile.NamedTemporaryFile 在临时文件前缀的末尾添加随机字符? - How to stop tempfile.NamedTemporaryFile from adding random characters to the end of the temp file prefix? Python> 3中的tempfile.TemporaryFile和tempfile.NamedTemporaryFile是否相同 - Are tempfile.TemporaryFile and tempfile.NamedTemporaryFile same in Python > 3 我可以在 python 中为 tempfile.NamedTemporaryFile 设置 umask 吗? - Can I set the umask for tempfile.NamedTemporaryFile in python? 为什么tempfile.NamedTemporaryFile()会截断我的数据? - Why does tempfile.NamedTemporaryFile() truncate my data? Python 2.7 tempfile.NamedTemporaryFile返回类型为“实例”而不是“文件”的对象。 为什么? - Python 2.7 tempfile.NamedTemporaryFile returns object in type 'instance' not 'file'. Why? python 临时文件 | NamedTemporaryFile 不能使用生成的临时文件 - python tempfile | NamedTemporaryFile can't use generated tempfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM