Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python problems with file names

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.

like image 641
gadmaget Avatar asked Jan 13 '23 14:01

gadmaget


1 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.

like image 171
unutbu Avatar answered Jan 17 '23 17:01

unutbu