how to open file explorer in python

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,', os.path.normpath(path)])

Here is what the above code is Doing:
1. It’s getting the path to the Windows Explorer executable.
2. It’s normalizing the path to the directory or file you want to open.
3. It’s checking if the path is a directory or a file.
4. If it’s a directory, it’s opening the directory in Explorer.
5. If it’s a file, it’s selecting the file in Explorer.