Open explorer on a file

前端 未结 5 2097
北海茫月
北海茫月 2020-11-28 03:21

In Python, how do I jump to a file in the Windows Explorer? I found a solution for jumping to folders:

import subprocess
subprocess.Popen(\'explorer \"C:\\pa         


        
5条回答
  •  北海茫月
    2020-11-28 04:02

    As explorer could be overridden it would be a little safer to point to the executable directly. (just had to be schooled on this too)

    And while you're at it: use Python 3s current subprocess API: run()

    import os
    import subprocess
    FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')
    
    def explore(path):
        # explorer would choke on forward slashes
        path = os.path.normpath(path)
    
        if os.path.isdir(path):
            subprocess.run([FILEBROWSER_PATH, path])
        elif os.path.isfile(path):
            subprocess.run([FILEBROWSER_PATH, '/select,', path])
    

提交回复
热议问题