Search code examples
pythonpython-2.7special-characters

Special character path to os.chmod()


I have a file which contains some path with special char. When I read and give it to the os.chmod it didn't find a path:

fileNames.txt:

D:\DEV\spec_char\subtypes_and_built_in_macro ビルドイン Лестницы\teszt.xml

my code:

import os
from stat import S_IWRITE
with open('fileNames.txt', 'r') as f:
    read_data = f.readlines()   
    for data in read_data:
        os.chmod(data.strip(), S_IWRITE)

and get the following error:

    os.chmod(data.strip().decode('latin1'), S_IWRITE)
WindowsError: [Error 3] The system cannot find the path specified: u'D:\\DEV\\spec_char\\subtypes_and_built_in_macro \xef\xbe\x8b\xef\xbe\x9e\xef\xbe\x99\xef\xbe\x84\xef\xbe\x9e\xef\xbd\xb2\xef\xbe\x9d \xd0\x9b\xd0\xb5\xd1\x81\xd1\x82\xd0\xbd\xd0\xb8\xd1\x86\xd1\x8b\\teszt.xml'

I have some files which is read-only and I want to remove this properties.


Solution

  • I use decode('utf-8') and now It's work fine

     os.chmod(data.strip().decode('utf-8'), S_IWRITE)