Note that there are some explanatory texts on larger screens.

plurals
  1. POPython 3 doesn't read unicode file on a new server
    text
    copied!<p>My webpages are served by a script that dynamically imports a bunch of files with</p> <pre><code>try: with open (filename, 'r') as f: exec(f.read()) except IOError: pass </code></pre> <p>(actually, can you suggest a better method of importing a file? I'm sure there is one.)</p> <p>Sometimes the files have strings in different languages, like</p> <pre><code># contents of language.ru title = "Название" </code></pre> <p>Those were all saved as UTF-8 files. Python has no problem running the script in command line or serving a page from my MacBook:</p> <pre> OK: [server command line] python3.0 page.py /index.ru OK: http://whitebox.local/index.ru </pre> <p>but it throws an error when trying to serve a page from a server we just moved to:</p> <pre> 157 try: 158 with open (filename, 'r') as f: 159 exec(f.read()) 160 except IOError: pass 161 /usr/local/lib/python3.0/io.py in read(self=, n=-1) ... UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 627: ordinal not in range(128) </pre> <p>All the files were copied from my laptop where they were perfectly served by Apache. What is the reason?</p> <p><strong>Update:</strong> I found out the default encoding for <code>open()</code> is platform-dependent so it was <code>utf8</code> on my laptop and <code>ascii</code> on server. I wonder if there is a per-program function to set it in Python 3 (<code>sys.setdefaultencoding</code> is used in <code>site</code> module and then deleted from the namespace).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload