Thank you. That works very well when writing to a text file but what is
the equivalent when writing the information to stdout using print?<br><br>Sorry when I originally replied I sent it directly and it didn't go to the list. <br><br><div class="gmail_quote">On Thu, Jun 25, 2009 at 12:57 AM, Mark Tolonen <span dir="ltr"><<a href="mailto:metolone%2Bgmane@gmail.com">metolone+gmane@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
"Amos Anderson" <<a href="mailto:amosanderson@gmail.com" target="_blank">amosanderson@gmail.com</a>> wrote in message news:a073a9cf0906242007k5067314dn8e9d7b1c6da6286a@mail.gmail.com...<div><div></div><div class="h5">
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I've run into a bit of an issue iterating through files in python 3.0 and<br>
3.1rc2. When it comes to a files with '\u200b' in the file name it gives the<br>
error...<br>
<br>
Traceback (most recent call last):<br>
 File "ListFiles.py", line 19, in <module><br>
   f.write("file:{0}\n".format(i))<br>
 File "c:\Python31\lib\encodings\cp1252.py", line 19, in encode<br>
   return codecs.charmap_encode(input,self.errors,encoding_table)[0]<br>
UnicodeEncodeError: 'charmap' codec can't encode character '\u200b' in<br>
position<br>
30: character maps to <undefined><br>
<br>
Code is as follows...<br>
import os<br>
f = open("dirlist.txt", 'w')<br>
<br>
for root, dirs, files in os.walk("C:\\Users\\Filter\\"):<br>
   f.write("root:{0}\n".format(root))<br>
   f.write("dirs:\n")<br>
   for i in dirs:<br>
       f.write("dir:{0}\n".format(i))<br>
   f.write("files:\n")<br>
   for i in files:<br>
       f.write("file:{0}\n".format(i))<br>
f.close()<br>
input("done")<br>
<br>
The file it's choking on happens to be a link that internet explorer<br>
created. There are two files that appear in explorer to have the same name<br>
but one actually has a zero width space ('\u200b') just before the .url<br>
extension. In playing around with this I've found several files with the<br>
same character throughout my file system. OS: Vista SP2, Language: US<br>
English.<br>
<br>
Am I doing something wrong or did I find a bug? It's worth noting that<br>
Python 2.6 just displays this character as a ? just as it appears if you<br>
type dir at the windows command prompt.<br>
</blockquote>
<br></div></div>
In Python 3.x strings default to Unicode.  Unless you choose an encoding, Python will use the default system encoding to encode the Unicode strings into a file.  On Windows, the filesystem uses Unicode and supports the full character set, but cp1252 (on your system) is the default text file encoding, which doesn't support zero-width space.  Specify an encoding for the output file such as UTF-8:<br>

<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
f=open('blah.txt','w',encoding='utf8')<br>
f.write('\u200b')<br>
</blockquote></blockquote></blockquote>
1<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
f.close()<br>
</blockquote></blockquote></blockquote>
<br>
-Mark<br><font color="#888888">
<br>
<br>
-- <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>