Search code examples
pythonwindowsntfs

Python and windows filesystem with none-ascii characters


I want to write a folder on a windows system, Vista and Win7 with NTFS file systems. The folders may contain the characters å, ä and/or ö, "förjävligt" for example.

The python files and every string in it is currently in UTF-8, how do I convert it to suite the Windows file system?


Solution

  • If you're working with normal Python 2 strings, you can simply convert them to Unicode

    # -*- coding: utf-8 -*-
    normalString = "äöü"
    
    # Now convert to unicode. Specified encoding must match the file encoding
    # in this example. In general, you must specify how the bytes-only string
    # contained in "normalString" is encoded.
    unicodeString = unicode(normalString, "utf-8")
    
    with open(unicodeString, "w") as f:
        ...
    

    and create the files using those Unicode strings. Python (and indirectly the Windows API) will take care of the rest.