Open explorer on a file

PythonWindowsExplorer

Python Problem Overview


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:\path\of\folder"')

but I have no solution for files.

Python Solutions


Solution 1 - Python

From Geoff Chappell's The Windows Explorer Command Line

import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')

Solution 2 - Python

A nicer and safer solution (only in Windows unfortunately) is os.startfile().

When it's given a folder instead of a file, it will open Explorer.

Im aware that i do not completely answer the question since its not selecting a file, but using subprocess is always kind of a bad idea (for security reasons) and this solution may help other people.

Solution 3 - Python

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])

Solution 4 - Python

Alternatively, you could use the fileopenbox module of EasyGUI to open the file explorer for the user to click through and then select a file (returning the full filepath).

import easygui
file = easygui.fileopenbox()

Solution 5 - Python

For some reason, on windows 7 it always opens the users Path, for me following worked out:

import subprocess
subprocess.call("explorer C:\\temp\\yourpath", shell=True)

Solution 6 - Python

For anyone wondering how to use a variable in place of a direct file path. The code below will open explorer and highlight the file specified.

import subprocess
subprocess.Popen(f'explorer /select,{variableHere}')

The code below will just open the specified folder in explorer without highlighting any specific file.

import subprocess
subprocess.Popen(f'explorer "{variableHere}"')

Ive only tested on windows

Solution 7 - Python

Code To Open Folder In Explorer:

import os
import ctypes
SW_SHOWDEFAULT = 10
path_to_open = os.getenv('windir')
ctypes.windll.shell32.ShellExecuteW(0, "open", path_to_open, 0, 0, SW_SHOWDEFAULT)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKirill TitovView Question on Stackoverflow
Solution 1 - PythonBlair ConradView Answer on Stackoverflow
Solution 2 - PythonGuillaume LebretonView Answer on Stackoverflow
Solution 3 - PythonewerybodyView Answer on Stackoverflow
Solution 4 - PythonMacNutterView Answer on Stackoverflow
Solution 5 - Pythonuser1767754View Answer on Stackoverflow
Solution 6 - PythonStephan YazvinskiView Answer on Stackoverflow
Solution 7 - PythonPixelsuftView Answer on Stackoverflow