在 Python 中生成随机文件名的最佳方法的答案显示了如何在 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')
...