title: IOError while writing to file
body: <p>Im writing a program that changes my desktop background. It does this by reading a text file. If the text file says one of the BG file names it saves that one as my background, and writes the name of the other one to the file and closes it.</p>
  
  <p>I can't seem to get it to work.<br>
  Here is my code:</p>
  
  <pre><code>import sys, os, ctypes
  
  BGfile = open('C:\BG\BG.txt', 'r+' )
  BGread = BGfile.read()
  x=0
  if BGread == 'mod_bg.bmp':
      x = 'BGMATRIX.bmp'
      BGfile.write('BGMATRIX.bmp')
      BGfile.close()
  
  elif BGread == 'BGMATRIX.bmp':
      x = 'mod_bg.bmp'
      BGfile.write('mod_bg.bmp')
      BGfile.close()
  
  pathToImg = x
  SPI_SETDESKWALLPAPER = 20  
  ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToImg, 0)
  </code></pre>
  
  <p>When I use <code>"r+"</code> it gives me this error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\BG\BG Switch.py", line 13, in &lt;module&gt;
      BGfile.write('mod_bg.bmp')
  IOError: [Errno 0] Error
  </code></pre>
  
  <p>Which isn't helpful at all!<br>
  When I use <code>"w+"</code> it just erases what's already in the file.</p>
  
  <p>Can anyone tell me why I'm getting this weird error, and a possible way to fix it?</p>