Unicode Chars in Windows Path

Steve sreisscruz at gmail.com
Wed Apr 2 19:27:04 EDT 2014


Hi All,

I'm in need of some encoding/decoding help for a situation for a Windows Path that contains Unicode characters in it.

---- CODE ----

import os.path
import codecs
import sys

All_Tests = [u"c:\automation_common\Python\TestCases\list_dir_script.txt"]


for curr_test in All_Tests:                             
  print("\n raw : " + repr(curr_test) + "\n")
  print("\n encode : %s \n\n" ) %  os.path.normpath(codecs.encode(curr_test, "ascii"))
  print("\n decode : %s \n\n" ) %  curr_test.decode('string_escape')

---- CODE ----


Screen Output : 

 raw : u'c:\x07utomation_common\\Python\\TestCases\\list_dir_script.txt'

 encode : c:utomation_common\Python\TestCases\list_dir_script.txt

 decode : c:utomation_common\Python\TestCases\list_dir_script.txt


My goal is to have the properly formatting path in the output :

 
c:\automation_common\Python\TestCases\list_dir_script.txt


What is the "magic" encode/decode sequence here??

Thanks!

Steve



More information about the Python-list mailing list