title: How to escape / in filename dynamically in python
body: <p>I have a program which writes to certain files, where file name is generated on the fly and can contain special characters at random.</p>
  
  <p>Whenever there is a <code>/</code> in these special characters, code considers it to be a OS path separator for linux.</p>
  
  <p>How can I escape this dynamically?</p>
  
  <p>Demonstration of what I am running into:</p>
  
  <pre><code>import random
  special_chars = "$^&amp;%*^(&amp;/"
  selected = random.choice(special_chars)
  #selected = "/"
  with open(__file__ + "%s.txt" % selected) as f:
      f.write("Hello")
  </code></pre>
  
  <p>Runs very well when it is any other character than <code>/</code>, but when you set selected as <code>/</code>:
  Error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./a.py", line 7, in &lt;module&gt;
      with open(__file__ + "%s.txt" % selected, 'w') as f:
  IOError: [Errno 20] Not a directory: './a.py/.txt'
  </code></pre>
  
  <p>python 2.7 only please.</p>