Cannot open filename that has umlaute in python 2.7 & django 1.9
<p>I am trying doing a thing that goes through every file in a directory, but it crashes every time it meets a file that has an umlaute in the name. Like Ã¤.txt</p>

<p>the shortened code:</p>

<pre><code>import codecs
import os

for filename in os.listdir(WATCH_DIRECTORY):
    with codecs.open(filename, 'rb', 'utf-8') as rawdata:
        data = rawdata.readline()
        # ...
</code></pre>

<p>And then I get this:</p>

<pre><code>IOError: [Errno 2] No such file or directory: '\xc3\xa4.txt'
</code></pre>

<p>I've tried to encode/decode the filename variable with .encode('utf-8'), .decode('utf-8') and both combined. This usually leads to "ascii cannot decode blah blah"</p>

<p>I also tried unicode(filename) with and without encode/decode.</p>

<p>Soooo, kinda stuck here :)</p>

<p>You are opening a <em>relative directory</em>, you need to make them absolute.</p>

<p>This has nothing really to do with encodings; both Unicode strings and byte strings will work, especially when soured from <code>os.listdir()</code>.</p>

<p>However, <code>os.listdir()</code> produces just the base filename, not a path, so add that back in:</p>

<pre><code>for filename in os.listdir(WATCH_DIRECTORY):
    fullpath = os.path.join(WATCH_DIRECTORY, filename)
    with codecs.open(fullpath, 'rb', 'utf-8') as rawdata:
</code></pre>

<p>By the way, I recommend you use the <a href="https://docs.python.org/2/library/io.html#io.open" rel="nofollow"><code>io.open()</code> function</a> rather than <code>codecs.open()</code>. The <code>io</code> module is the new Python 3 I/O framework, and is a lot more robust than the older <code>codecs</code> module.</p>

python django python-2.7 encoding utf-8