Thanks a lot for this quick answer! It is very clear!<div><br></div><div>Ti better understand what the difference between encoding and decoding is I found the following website: <a href="http://www.evanjones.ca/python-utf8.html">http://www.evanjones.ca/python-utf8.html</a></div>
<div><br></div><div><a href="http://www.evanjones.ca/python-utf8.html"></a>I change the program to (changes are in bold):</div><div><div><i># -*- <b>coding: utf8</b> -*- </i>(no more cp1252 the source file is directly in unicode)</div>
<div><i>#!/usr/bin/python</i></div><div><i>'''</i></div><div><i>Created on 27 déc. 2010</i></div><div><i><br></i></div><div><i>@author: jpmena</i></div><div><i>'''</i></div><div><i>from datetime import datetime</i></div>
<div><i>import locale</i></div><div><i>import codecs</i></div><div><i>import os,sys</i></div><div><i><br></i></div><div><i>class Log(object):</i></div><div><i>    log=None</i></div><div><i>    def __init__(self,log_path):</i></div>
<div><i>        self.log_path=log_path</i></div><div><i>        if(os.path.exists(self.log_path)):</i></div><div><i>            os.remove(self.log_path)</i></div><div><i>        #self.log=open(self.log_path,'a')</i></div>
<div><i>        self.log=codecs.open(self.log_path, "a",<b> 'utf-8'</b>)</i></div><div><i>    </i></div><div><i>    def getInstance(log_path=None):</i></div><div><i>        print "encodage systeme:"+sys.getdefaultencoding()</i></div>
<div><i>        if Log.log is None:</i></div><div><i>            if log_path is None:</i></div><div><i>                log_path=os.path.join(os.getcwd(),'logParDefaut.log')</i></div><div><i>            Log.log=Log(log_path)</i></div>
<div><i>        return Log.log</i></div><div><i>    </i></div><div><i>    getInstance=staticmethod(getInstance)</i></div><div><i>        </i></div><div><i>    def p(self,msg):</i></div><div><i>        aujour_dhui=datetime.now()</i></div>
<div><i>        date_stamp=aujour_dhui.strftime("%d/%m/%y-%H:%M:%S")</i></div><div><i>        print sys.getdefaultencoding()</i></div><div><i>        unicode_str='%s : %s \n'  % (date_stamp,<b>unicode(msg,'utf-8')</b>)</i></div>
<div><i>        #unicode_str=msg</i></div><div><i>        self.log.write(unicode_str)</i></div><div><i>        return unicode_str</i></div><div><i>    </i></div><div><i>    def close(self):</i></div><div><i>        self.log.flush()</i></div>
<div><i>        self.log.close()</i></div><div><i>        return self.log_path</i></div><div><i><br></i></div><div><i>if __name__ == '__main__':</i></div><div><i>    l=Log.getInstance()</i></div><div><i>    l.p("premier message de Log à accents")</i></div>
<div><i>    Log.getInstance().p("second message de Log")</i></div><div><i>    l.close()</i></div><div><br></div><div>The DOS conole output is now:</div><div><div><i>C:\Documents and Settings\jpmena\Mes documents\VelocityRIF\VelocityTransforms>generationProgrammeSitePublicActuel.cmd</i></div>
<div><i>Page de codes active : 1252</i></div><div><i>encodage systeme:ascii</i></div><div><i>ascii</i></div><div><i>encodage systeme:ascii</i></div><div><i>ascii</i></div></div><div><br></div><div>And the Generated Log file showsnow the expected result:</div>
<div><div><i>11/04/11-10:53:44 : premier message de Log à accents </i></div><div><i>11/04/11-10:53:44 : second message de Log</i></div></div><div><br></div><div>Thanks.</div><div><br></div><div>If you have other links of interests about unicode encoding and decoding  in Python. They are welcome</div>
<br><div class="gmail_quote">2011/4/10 MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">On 10/04/2011 13:22, Jean-Pierre M wrote:<br>
> I created a simple program which writes in a unicode files some french text with accents!<br></div>
[snip]<br>
This line:<div class="im"><br>
<br>
    l.p("premier message de Log à accents")<br>
<br></div>
passes a bytestring to the method, and inside the method, this line:<div class="im"><br>
<br>
    unicode_str=u'%s : %s \n'  % (date_stamp,msg.encode(self.charset_log,'replace'))<br>
<br></div>
it tries to encode the bytestring to Unicode.<br>
<br>
It's not possible to encode a bytestring, only a Unicode string, so<br>
Python tries to decode the bytestring using the fallback encoding<br>
(ASCII) and then encode the result.<br>
<br>
Unfortunately, the bytestring isn't ASCII (it contains accented<br>
characters), so it can't be decoded as ASCII, hence the exception.<br>
<br>
BTW, it's probably better to forget about cp1252, etc, and use UTF-8<br>
instead, and also to use Unicode wherever possible.<br><font color="#888888">
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br></div>