4

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, @unutbu. This worked with a little bit of tweaking.
4

str.encode("unicode_escape") will encode a string in the way you described.

>>> print(u"Hello WorlЪ.py".encode("unicode_escape"))
Hello Worl\u042a.py

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.