生成临时文件名而无需在 Python 中创建实际文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26541416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate temporary file names without creating actual file in Python
提问by Hill
The question, number 10501247, in stackoverflow gives answer how to create temporary file in Python.
I only need to have temporary file name in my case.
Calling tempfile.NamedTemporaryFile() returns file handle after actual file creation.
Is there way to get file name only?
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')
...
回答by Marcin
If you want a temp file name only you can call inner tempfile function _get_candidate_names():
如果你只想要一个临时文件名,你可以调用内部临时文件函数_get_candidate_names():
import tempfile
temp_name = next(tempfile._get_candidate_names())
% e.g. px9cp65s
Calling nextagain, will return another name, etc. This does not give you the path to temp folder. To get default 'tmp' directory, use:
next再次调用,将返回另一个名称等。这不会为您提供临时文件夹的路径。要获取默认的“tmp”目录,请使用:
defult_tmp_dir = tempfile._get_default_tempdir()
% results in: /tmp
回答by PM 2Ring
As Joachim Isaksson said in the comments, if you just get a name you may have problems if some other program happens to use that name before your program does. The chances are slim, but not impossible.
正如 Joachim Isaksson 在评论中所说的那样,如果您只是获得一个名称,那么如果其他程序碰巧在您的程序使用该名称之前使用该名称,则您可能会遇到问题。机会渺茫,但并非不可能。
So the safe thing to do in this situation is to use the full GzipFile() constructor, which has the signature GzipFile( [filename[, mode[, compresslevel[, fileobj]]]]). So you can pass it the open fileobj, and a filename as well, if you like. See the gzip docs for details.
因此,在这种情况下,安全的做法是使用完整的 GzipFile() 构造函数,它具有签名GzipFile( [filename[, mode[, compresslevel[, fileobj]]]]). 因此,如果您愿意,您可以将打开的 fileobj 和文件名传递给它。有关详细信息,请参阅 gzip 文档。
回答by russell
It may be a little late, but is there anything wrong with this?
可能有点晚了,但这有什么问题吗?
import tempfile
with tempfile.NamedTemporaryFile(dir='/tmp', delete=False) as tmpfile:
temp_file_name = tmpfile.name
f = gzip.open(temp_file_name ,'wb')
回答by Zitrax
tempfile.mktemp()do this.
But note that it's deprecated. However it will notcreate the file and it is a public function in tempfile compared to using the _get_candidate_names().
但请注意,它已被弃用。但是,它不会创建文件,并且与使用_get_candidate_names().
The reason it's deprecated is due to the time gap between calling this and actually trying to create the file. However in my case the chance of that is so slim and even if it would fail that would be acceptable. But it's up to you to evaluate for your usecase.
不推荐使用它的原因是调用它和实际尝试创建文件之间的时间间隔。但是,在我的情况下,这种可能性很小,即使失败也是可以接受的。但是由您来评估您的用例。
回答by Alec
I think the easiest, most secure way of doing this is something like:
我认为最简单、最安全的方法是:
path = os.path.join(tempfile.mkdtemp(), 'something')
A temporary directory is created that only you can access, so there should be no security issues, but there will be no files created in it, so you can just pick any filename you want to create in that directory.
创建了一个只有您可以访问的临时目录,因此应该没有安全问题,但其中不会创建任何文件,因此您可以选择要在该目录中创建的任何文件名。
回答by juanmirocks
Combining the previous answers, my solution is:
结合之前的答案,我的解决方案是:
def get_tempfile_name(some_id):
return os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + "_" + some_id)
Make some_idoptional if not needed for you.
some_id如果您不需要,请将其设为可选。

