如果你希望指定具体的目录和文件名,你可以在 NamedTemporaryFile
中使用 dir
和 suffix
参数。以下是一个例子:
import tempfile
import os
# 指定目录和文件名
directory = "/path/to/your/directory"
filename = "custom_filename.png"
# 使用 NamedTemporaryFile 指定目录和文件名
with tempfile.NamedTemporaryFile(prefix="vnc_", suffix=".png", dir=directory, delete=False) as png:
# 获取完整的文件路径
full_path = os.path.join(directory, filename)
os.rename(png.name, full_path)
# 在这里使用 full_path 进行其他操作
print("Custom file created at:", full_path)
请替换 "/path/to/your/directory"
和 "custom_filename.png"
为你想要的目录和文件名。在这个例子中,full_path
就是你指定的目录和文件名的完整路径