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

52 Views Asked by At

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.

However, because of the % at the beginning, it makes python unable to identify it. I tried to add double %%, but that didn't work as well.

What should I do?

1

There are 1 best solutions below

2
wim On

You should use the tempfile module instead:

import tempfile
my_temp = tempfile.NamedTemporaryFile()

This is the platform-independent way to have a temporary file.