title: Python3 str(), bytes, and unicode
body: <p>I'm having trouble with the TYPES involved with this piece of code I wrote.  Ideally I wouldn't pay any mind to encoding types, but sometimes you're forced.</p>
  
  <p>So this is all centered around a directory walk of an NTFS FS on Windows.  Certain characters in file names (unicode, it seems) couldn't be written out to files or printed to the standard windows terminal (yes, I tried "chcp 65001" to print, which didn't work, but I need to write to a standard plain text file anyway)</p>
  
  <p>So I do the following.  As I understand it Python3 (I'm using 3.2.2) is unicode, so str() objects (and all supporting libs) are unicode, so I did this:</p>
  
  <pre><code>absfilepath = os.path.join(root, file).encode()
  </code></pre>
  
  <p>thinking utf-8 string would be returned and all is good with the world, but then I was getting errors about implicit type conversions to <code>str()</code> when I went to file write or stdout.  So I did the following:</p>
  
  <pre><code>hashmap[checksum] = str(absfilepath)
  </code></pre>
  
  <p>(the hashmap is dumped later).</p>
  
  <p>thinking now it's in a native unicode Python3 string...but when I dump it into a file, using this:</p>
  
  <pre><code>for key, val in m.items():
      f.write(key + "|" + val + "\n")
  </code></pre>
  
  <p>I still get this in the file:</p>
  
  <pre><code>e77bceb64d179377731a94186e56281c|b'K:\Filename'
  </code></pre>
  
  <p>which is indicative as a byte array.</p>
  
  <p>So what am I doing wrong here?  I'm sorry 'non-traditional' characters are in this directory tree, I'd rather them not be there, but they're there.  How do I store them (convert them?) into a manner that can be printed/written in normal plain text (ASCII?) and why is a byte array being returned from my hashmap where I'm clearly storing a standard string?  Dealing with unicode has been a pretty horrific experience for me.</p>