Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'output': 0.05; '-*-': 0.07; 'binary': 0.07; 'python3': 0.07; 'utf-8': 0.07; 'string': 0.09; 'bash': 0.09; 'coding:': 0.09; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'locale': 0.09; 'parameter': 0.09; 'template': 0.14; 'encoding.': 0.16; 'mode,': 0.16; 'os.walk': 0.16; 'paulo': 0.16; 'subject:Problem': 0.16; 'subject:writing': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'pfxlen:0': 0.19; 'work,': 0.20; 'command': 0.22; 'this?': 0.23; 'bytes': 0.24; 'replace': 0.24; 'skip:l 30': 0.24; 'file.': 0.24; 'source': 0.25; 'script': 0.25; 'this:': 0.26; '(for': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'skip:- 40': 0.29; 'leave': 0.29; 'mode': 0.30; 'specified': 0.30; 'skip:( 20': 0.30; 'subject:some': 0.31; 'text': 0.33; 'open': 0.33; 'linux': 0.33; 'header:Received:9': 0.33; 'raw': 0.33; 'date:': 0.34; 'problem': 0.35; 'subject: (': 0.35; 'something': 0.35; 'thanks': 0.36; 'january': 0.37; 'email addr:python.org': 0.37; 'to:addr:python-list': 0.38; 'subject:': 0.39; '12,': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'received:98.137': 0.60; 'email name:python-list': 0.65; 'default': 0.69; 'special': 0.74; '2014,': 0.84; 'received:98.138.229': 0.84; 'received:98.138.226': 0.93 X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 525323.96473.bm@omp1040.mail.gq1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1389543550; bh=xgyR17OqVLSq5bkobsMOGITCd1u315IYlIbm0IWuqK8=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=beK3+Kwjbld0vrelmRCegRI/cwhIJycj2Ia5ZWN/2LPf0vg3yZ6ECSEPqEyJpwC+jM3043YpCn7I6IpShbBV3znPFtZbS4LI7t8R0V2lqk1qXLF2b9obfq2UPdaBsQ9J+aH6kwYhbgyqQJsVFXg2jMWiMOeBmWtMubTF90uMxho= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:Message-ID:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=ySjpkaOIFC19Rq/g/HHB2IMl/fMURd+QSFFWhWQctq9rPxczFda3r4qd/aqOUlf74TWqML4dgs0t/N7sPbUWG3Idk9KkU99cEUu21WYiCn3jsRCuCMDp3hhVQhxNqoyfF682mTZn1tQ8JgcAxD61+RhoqfJRmbeyOonKUmZVVR8=; X-YMail-OSG: kGhJYioVM1n53.Qr7pP63vDSXfGv.EfDWFsa97ezlil6TkN KJV7mqTf.JO6OM4k.qHdkGd7jqEmfBdzX.kSt.QYNZqWnOsVRQsZcm3KU2OF KJbMHfiYwhd_cw.4EdFVgIATH.9pzefdDnyypv4u58JJuH8T3e0KK0.ZVKQU 1Y9N0_UlMuLQ1ZWJladtKRNqwVn3QDczo8j6Ys2LPZxjT7Db6.qBgI8Eykgg bTi6w0EeD5KbJs9c88BlvzCFraM_eyI.FbooJTXTRi5FmnNGVQw3wGJYNps_ 0zoOSei.rZbRDdS71A6pl.HIm4rlXx7w0CImSySgzwvNfyrCPpvKR_x5ACZ6 VnkNi4_.PYseLc9Zze.hHM1usWfvJ_rorRyRk9AeKY5pMEgEp4WS5Jsx_1Ll gHC39FVYzFxvwVRL6f0HGaThUiAxSGkz8mkGymD3zECa5YRLYP0d5CL_CEaC R.zEVriA3fNIOkhVVKqW4zKx2vAlzi5vwUAclp6hK_35TGdNQmFV4gjYpT1j IpeIRbgXA.3IRKU9sCo3vHKCjcxcDXJLl5XPfyeEd2n42YkchC3iy5eKrnh0 d7PHn2YSotME0HmU8hZg- X-Rocket-MIMEInfo: 002.001, LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0NCk9uIFN1biwgMS8xMi8xNCwgUGF1bG8gZGEgU2lsdmEgPHBfc19kX2Ffc19pX2xfdl9hQG5ldGNhYm8ucHQ.IHdyb3RlOg0KDQogU3ViamVjdDogUHJvYmxlbSB3cml0aW5nIHNvbWUgc3RyaW5ncyAoVW5pY29kZUVuY29kZUVycm9yKQ0KIFRvOiBweXRob24tbGlzdEBweXRob24ub3JnDQogRGF0ZTogU3VuZGF5LCBKYW51YXJ5IDEyLCAyMDE0LCA0OjM2IFBNDQogDQogSGkhDQogDQogSSBhbSB1c2luZyBhIHB5dGhvbjMgc2NyaXABMAEBAQE- X-Mailer: YahooMailClassic/387 YahooMailWebService/0.8.172.614 Date: Sun, 12 Jan 2014 08:19:10 -0800 (PST) From: Albert-Jan Roskam Subject: Re: Problem writing some strings (UnicodeEncodeError) To: python-list@python.org, Paulo da Silva In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389543733 news.xs4all.nl 2840 [2001:888:2000:d::a6]:60097 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63779 -------------------------------------------- On Sun, 1/12/14, Paulo da Silva wrote: Subject: Problem writing some strings (UnicodeEncodeError) To: python-list@python.org Date: Sunday, January 12, 2014, 4:36 PM =20 Hi! =20 I am using a python3 script to produce a bash script from lots of filenames got using os.walk. =20 I have a template string for each bash command in which I replace a special string with the filename and then write the command to the bash script file. =20 Something like this: =20 shf=3Dopen(bashfilename,'w') filenames=3Dgetfilenames() # uses os.walk for fn in filenames: =A0=A0=A0 ... =A0=A0=A0 cmd=3Dtempl.replace("",fn) =A0=A0=A0 shf.write(cmd) =20 For certain filenames I got a UnicodeEncodeError exception at shf.write(cmd)! I use utf-8 and have # -*- coding: utf-8 -*- in the source .py. =20 How can I fix this? =20 Thanks for any help/comments. =20 =3D=3D=3D=3D=3D=3D> what is the output of locale.getpreferredencoding(False= )? That is the default value of the "encoding" parameter of the open functi= on. shf=3Dopen(bashfilename,'w', encoding=3D'utf-8') might work, though on my = Linux macine locale.getpreferredencoding(False) returns utf-8. help(open) ... In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) ...