Search code examples
pythonunicodewindows-explorer

Open and Select Item in Windows Explorer (Unicode) - Python


I know how to open and select file in windows explorer by using explorer.exe commandline options '/n,@/select ' but I can only make it work with regular characters. Anyone have idea how to make it support unicode characters such as this 五輪代? I've tried to encode it with 'utf-8' but it didn't work, I'm sure there's a proper way to do it I just don't know how I hope someone can give me idea. Thanks in advance! :)

here's my sample code:

import win32api

win32api.ShellExecute(None, 'open', 'explorer.exe',
                      '/n,@/select, ' + file_path, None, 1)

Solution

  • You can use ctypes to access the API in a more direct way: (file_path should be a Python unicode object, not utf-8)

    import ctypes
    ctypes.windll.shell32.ShellExecuteW(None, u'open', u'explorer.exe', 
                                        u'/n,/select, ' + file_path, None, 1)