Python findall, regex, unicode
Question: I'm trying to write a Python script that searches thru a directory tree and
lists all .flac files and derives Arist, Album and Title from resp.
dir/subdir/filename and write that to a file. The code works fine, until it
hits a unicode character. Here's the code:

    
    
    import os, glob, re
    
    def scandirs(path):
        for currentFile in glob.glob(os.path.join(path, '*')):
        if os.path.isdir(currentFile):
            scandirs(currentFile)
        if os.path.splitext(currentFile)[1] == ".flac":
            rpath = os.path.relpath(currentFile)
            print "**DEBUG** rpath =", rpath
            title = os.path.basename(currentFile)
            title = re.findall(u'\d\d\s(.*).flac', title, re.U)
            title = title[0].decode("utf8")
            print "**DEBUG** title =", title
            fpath = os.path.split(os.path.dirname(currentFile))
            artist = fpath[0][2:]
            print "**DEBUG** artist =", artist
            album = fpath[1]
            print "**DEBUG** album =", album
            out = "%s | %s | %s | %s\n" % (rpath, artist, album, title)
            flist = open('filelist.tmp', 'a')
            flist.write(out)
            flist.close()
    
    scandirs('./')
    

code output:

    
    
    **DEBUG** rpath = Thriftworks/Fader/Thriftworks - Fader - 01 180°.flac
    **DEBUG** title = 180°
    **DEBUG** artist = Thriftworks
    **DEBUG** album = Fader
    Traceback (most recent call last):
      File "decflac.py", line 25, in <module>
        scandirs('./')
      File "decflac.py", line 7, in scandirs
        scandirs(currentFile)
      File "decflac.py", line 7, in scandirs
        scandirs(currentFile)
      File "decflac.py", line 20, in scandirs
        out = "%s | %s | %s | %s\n" % (rpath, artist, album, title)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 46: ordinal not in range(128)
    

But when tried in Python console, it is working fine:

    
    
    >>> import re
    >>> title = "Thriftworks - Fader - 01 180°.flac"
    >>> title2 = "dummy"
    >>> title = re.findall(u'\d\d\s(.*).flac', title, re.U)
    >>> title = title[0].decode("utf8")
    >>> out = "%s | %s\n" % (title2, title)
    >>> print out
    dummy | 180°
    

So, my questions: 1) How come the same code works in the console, but not in
the script? 2) How to fix the script?


Answer: Python console works with your terminal and interprets unicode encoding
according to its locale.

Replace the line with the new `str.format`:

    
    
    out = u"{} | {} | {} | {}\n".format(rpath, artist, album, title)
    

And encode to utf8 when writing to a file:

    
    
    with open('filelist.tmp', 'a') as f:
        f.write(out.encode('utf8'))
    

or `import codecs` and do directly:

    
    
    with codecs.open('filelist.tmp', 'a', encoding='utf8') as f:
        f.write(out)
    

or, since utf8 is default:

    
    
    with open('filelist.tmp', 'a') as f:
        f.write(out)
    

