import os # c:\äöü\test.txt if os.path.sep == '\\': path_byte_dir = b'c:\\\xe4\xf6\xfc' path_byte = b'c:\\\xe4\xf6\xfc//test.txt' else: path_byte_dir = b'/tmp/\xe4\xf6\xfc' path_byte = b'/tmp/\xe4\xf6\xfc//test.txt' print("Testing...") if not os.path.exists(path_byte_dir): os.mkdir(path_byte_dir) path_utf = path_byte.decode('utf8', 'surrogateescape') # write f = open(path_byte, 'w') f.write("testing\n") f.close() # This wont fail, just to show conversion is correct. if path_byte != path_utf.encode('utf8', 'surrogateescape'): raise Exception("error converting bacl") # read as surrogate escaoped. raises an error on windows xp but not linux # IOError: [Errno 2] No such file or directory: 'c:\\\udce4\udcf6\udcfc//test.txt' f = open(path_utf, 'r') print("It Worked!")