title: Write to %temp% with python?
body: <p>I have a small program that works fine on my PC but I want to make it portable. What it does is download an image from the internet, set as desktop background, wait one minute and update the image. I know that I cannot write directly to folders like appdata, as I do not know the username of the person using the computer. I need to save the downloaded image somewhere, so I would save it in the windows <code>Temp</code> folder.</p>
  
  <p>Some options I think would be to (However I don't know how to do this in python)</p>
  
  <ul>
  <li>Use something like <code>%temp%</code> to access the folder.</li>
  <li>Find out the username of the person running the program and insert into path</li>
  <li>Use a variable for the username.</li>
  <li>Use relative paths</li>
  </ul>
  
  <p>I would like to try and not have to use another module not by default included in Python 3, as I want to cx_freeze it later on.</p>
  
  <pre><code>import pythoncom
  from urllib import request
  from win32com.shell import shell, shellcon
  from time import sleep
  
  def get_image():
      f = open('C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg', 'wb') #Open old image
      f.write(request.urlopen('blalbla.com/foo/img.jpg').read()) #Get new image and write
      f.close()
  
  pathtoimg = 'C:\\Users\\MyUser\\Desktop\\Python\\bg\\bg.jpg'
  
  count = 0
  while 1:
      get_image()
      iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, None,
            pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IActiveDesktop)
      iad.SetWallpaper(pathtoimg, 0)
      iad.ApplyChanges(shellcon.AD_APPLY_ALL)
      count += 1
      print(count)
      sleep(60)
  </code></pre>