Unable to print the files with special characters while using python
<p>I developed a web crawler to extract all the source codes in a wiki link. The program terminates after writing a few files.</p>

<pre><code> def fetch_code(link_list):
    for href in link_list:
        response = urllib2.urlopen("https://www.wikipedia.org/"+href)
        content = response.read()
        page = open("%s.html" % href, 'w')
        page.write(content.replace("[\/:?*&lt;&gt;|]", " "))
        page.close()
</code></pre>

<p><code>link_list</code> is an array, which has the extracted links from the seed page.</p>

<p>The error I get after executing is </p>

<pre><code>IOError: [Errno 2] No such file or directory: u'M/s.html'
</code></pre>

<p>you cannot create a file with '/' in its name.</p>

<p>you could escape the filename as M%2Fs.html</p>

<p>/ is %2F</p>

<p>in python2, you could simply use urllib to escape the filename, example:</p>

<pre><code>import urllib

filePath = urllib.quote_plus('M/s.html')

print(filePath)
</code></pre>

<p>on the other hand, you could also save http response to hierarchy, for example, M/s.html means s.html file under directory named 'M'.</p>

python