Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting file name with Unicode characters in windows

Python version: 2.7.3

Filename: test snowman character --☃--.mp3

Ran the following tests, None of them proved successful.

>>> os.path.exist('test snowman character --☃--.mp3')
False
>>> os.path.exist(repr('test snowman character --☃--.mp3'))
False
>>> os.path.isfile('test snowman character --\\xe2\\x98\\x83--.mp3')
False
>>> os.path.isfile(r'test snowman character --\\xe2\\x98\\x83--.mp3')
False
>>> os.path.isfile('test snowman character --☃--.mp3'.decode('utf-8'))
False

Tried to retrieve files with glob, even that test failed.

Objective is to detect and copy this file to another folder, Please Advise.

like image 506
Karthikeyan S Avatar asked Oct 02 '22 14:10

Karthikeyan S


1 Answers

Use a unicode value; preferably with a unicode escape sequence:

os.path.isfile(u'test snowman character --\u2603--.mp3')

Python on Windows will use the correct Windows API for listing UTF16 files when you give it a unicode path.

For more information on how Python alters behaviour with unicode vs. bytestring file paths, see the Python Unicode HOWTO.

like image 72
Martijn Pieters Avatar answered Oct 13 '22 12:10

Martijn Pieters