python open windows explorer -


please image such situation: local file's icon displayed in gui, right click icon, context menu pops, option: show file in explorer. click option, explorer window opened, particular file selected. many editors have such feature: show in folder or show in explorer

in fact, gui built pyqt, first thought simple, open subprocess , pass command line:

explorer /select, a_full_path_name

the behavior indeed need, when click "show in folder" again, new explorer window opened, old 1 exsits! how naughty boy clicking "show in folder" dozens of times in breath? need 1 window, if old 1 exists, raise front.

the command start /d a_path . may disappoint naughty boy(run several times, 1 window.) however, there no option highlight selected file, disappoint me...

as mentioned above, many editors have such "show in folder" feature, suprise, pycharm "show in explorer" open multiple windows multiple clicks on same file, , codeblocks "opening containing folder", programmer's notepad "open containing folder" open 1 folder on same file.(to honest, have 3 editors in pc except windows notepad :)


my question:
can feature mentioned above achieved windows cmd?
if can not, there python way achieve that?

in fact, found several related questions in stackoverflow, for example, problem unsolved, give me ride?

finally, nice guy guided me answer.
it's https://github.com/exaile/exaile/blob/master/xl/common.py#l350

in py3+

import ctypes  ctypes.windll.ole32.coinitialize(none) upath = r"c:\windows" pidl = ctypes.windll.shell32.ilcreatefrompathw(upath) ctypes.windll.shell32.shopenfolderandselectitems(pidl, 0, none, 0) ctypes.windll.shell32.ilfree(pidl) ctypes.windll.ole32.couninitialize() 

in py2+

just give unicode path.
note: ilcreatefrompathw (unicode) , ilcreatefrompatha (ansi)


Comments