Hil*_*ill 71 python temporary-files
stackoverflow中的问题编号10501247给出了如何在Python中创建临时文件的答案.
在我的情况下,我只需要有临时文件名.
调用tempfile.NamedTemporaryFile()会在实际创建文件后返回文件句柄.
有办法获取文件名吗?
# Trying to get temp file path
tf = tempfile.NamedTemporaryFile()
temp_file_name = tf.name
tf.close()
# Here is my real purpose to get the temp_file_name
f = gzip.open(temp_file_name ,'wb')
...
Run Code Online (Sandbox Code Playgroud)
Mar*_*cin 55
如果只想要临时文件名,可以调用内部tempfile函数_get_candidate_names():
import tempfile
temp_name = next(tempfile._get_candidate_names())
% e.g. px9cp65s
Run Code Online (Sandbox Code Playgroud)
next再次调用,将返回另一个名称等.这不会为您提供临时文件夹的路径.要获取默认的'tmp'目录,请使用:
defult_tmp_dir = tempfile._get_default_tempdir()
% results in: /tmp
Run Code Online (Sandbox Code Playgroud)
Ale*_*lec 29
我认为最简单,最安全的方法是这样的:
path = os.path.join(tempfile.mkdtemp(), 'something')
Run Code Online (Sandbox Code Playgroud)
创建只有您可以访问的临时目录,因此不存在安全问题,但不会在其中创建任何文件,因此您只需选择要在该目录中创建的任何文件名.
rus*_*ell 12
可能有点晚了,但这有什么不对吗?
import tempfile
with tempfile.NamedTemporaryFile(dir='/tmp', delete=False) as tmpfile:
temp_file_name = tmpfile.name
f = gzip.open(temp_file_name ,'wb')
Run Code Online (Sandbox Code Playgroud)
我们尽量不要想太多:
import os, uuid, tempfile as tf
def make_temp_name(dir = tf.gettempdir()):
return os.path.join(dir, str(uuid.uuid1()))
Run Code Online (Sandbox Code Playgroud)
tempfile.mktemp() 做这个.
但请注意,它已被弃用.但是它不会创建文件,并且与使用文件相比,它是tempfile中的公共函数_get_candidate_names().
它被弃用的原因是由于调用它和实际尝试创建文件之间的时间差.然而在我的情况下,这种情况的可能性非常小,即使它会失败也是可以接受的.但是由你来评估你的用例.
正如 Joachim Isaksson 在评论中所说,如果您只是获得一个名称,如果其他程序碰巧在您的程序之前使用该名称,则可能会遇到问题。机会很小,但并非不可能。
因此,在这种情况下,安全的做法是使用完整的GzipFile()构造函数,它具有签名
GzipFile( [filename[, mode[, compresslevel[, fileobj]]]])
Run Code Online (Sandbox Code Playgroud)
因此,如果您愿意,您可以将打开的文件对象和文件名传递给它。有关详细信息,请参阅gzip 文档。
结合之前的答案,我的解决方案是:
def get_tempfile_name(some_id):
return os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + "_" + some_id)
Run Code Online (Sandbox Code Playgroud)
some_id如果您不需要,请将其设为可选。
我会这样做:
import tempfile
import os.path
import random
import string
def generate_temp_filename() -> str:
random_string = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
return os.path.join(tempfile.gettempdir(), random_string)
Run Code Online (Sandbox Code Playgroud)
相对于其他答案的优势:
_)| 归档时间: |
|
| 查看次数: |
31332 次 |
| 最近记录: |