-------------------------------------------- On Sun, 1/12/14, Paulo da Silva <p_s_d_a_s_i_l_...@netcabo.pt> wrote:
Subject: Problem writing some strings (UnicodeEncodeError) To: python-list@python.org Date: Sunday, January 12, 2014, 4:36 PM 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>",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? Thanks for any help/comments. ======> what is the output of locale.getpreferredencoding(False)? That is the default value of the "encoding" parameter of the open function. shf=open(bashfilename,'w', encoding='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.) ... -- https://mail.python.org/mailman/listinfo/python-list