Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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; '-*-': 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; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'template': 0.14; 'exactly,': 0.16; 'os.walk': 0.16; 'paulo': 0.16; 'received:173.11': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:Problem': 0.16; 'subject:writing': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'command': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'file.': 0.24; 'source': 0.25; 'script': 0.25; 'this:': 0.26; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:some': 0.31; "i'd": 0.34; 'subject: (': 0.35; 'something': 0.35; 'but': 0.35; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'received:173': 0.61; 'special': 0.74; 'start.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Emile van Sebille Subject: Re: Problem writing some strings (UnicodeEncodeError) Date: Sun, 12 Jan 2014 08:55:12 -0800 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 173-11-108-137-sfba.hfc.comcastbusiness.net User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389546006 news.xs4all.nl 2896 [2001:888:2000:d::a6]:32774 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63788 On 01/12/2014 07:36 AM, Paulo da Silva wrote: > Hi! > > I am using a python3 script to produce a bash script from lots of > filenames got using os.walk. > > 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. > > Something like this: > > shf=open(bashfilename,'w') > filenames=getfilenames() # uses os.walk > for fn in filenames: > ... > cmd=templ.replace("",fn) > shf.write(cmd) > > 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. > > How can I fix this? Not sure exactly, but I'd try shf=open(bashfilename,'wb') as a start. HTH, Emile