title: Generate almost random file name in Python
body: <p>I have</p>
  
  <pre><code>def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
       return ''.join(random.choice(chars) for x in range(size))
  r = id_generator()
  bat = open('MS-%s.bat',"w")%(r)
  </code></pre>
  
  <p>Why doesn't this work?</p>
  
  <pre><code>TypeError: unsupported operand type(s) for %: 'file' and 'str'
  </code></pre>