I'd like to find a function that accesses a directory using the os.listdir() function (or any other method) and returns all the file names in that directory but converts non-ASCII characters into their unicode format. For example, if I had the file Hello WorlЪ.py, I'd like for the function to return Hello Worl\u042a.py or something equivalent. Any help is appreciated.
Add a comment
|
2 Answers
If you pass os.listdir a unicode path, then os.listdir returns unicode:
os.listdir(u'.')
From the docs:
Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects.
1 Comment
gadmaget
Thank you, @unutbu. This worked with a little bit of tweaking.