简体   繁体   English

如何在Win 7中将temp目录添加为Python变量

[英]How to add the temp directory in Win 7 as a Python variable

The temp directory in win 7 is %userprofile%\\AppData\\Local\\Temp and I'd like to add it to a python script as a variable to be used to create a text file there. Win 7中的临时目录为%userprofile%\\AppData\\Local\\Temp ,我想将其作为变量添加到python脚本中,以用于在此处创建文本文件。

However, because of the % at the beginning, it makes python unable to identify it. 但是,由于开头是% ,它使python无法识别它。 I tried to add double %% , but that didn't work as well. 我试图添加%% ,但是效果不佳。

What should I do? 我该怎么办?

You should use the tempfile module instead: 您应该改为使用tempfile模块:

import tempfile
my_temp = tempfile.NamedTemporaryFile()

This is the platform-independent way to have a temporary file. 这是具有临时文件的独立于平台的方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM