Python: How to open a folder on Windows Explorer(Python 3.6.2, Windows 10)
Asked Answered
L

6

28

If I store the path that i want to open in a string called finalpath which looks something like this: "./2.8 Movies/English/Die Hard Series"

then how do i open this in Windows Explorer?(Windows 10)(Python 3.6.2)

P.S I know many people have asked this question but I did not find them clear. Please answer soon.

Luminiferous answered 14/12, 2017 at 11:35 Comment(1)
Edit queue is full unfortunately.Overshoe
L
62

I found a simple method.

import os
path = "C:/Users"
path = os.path.realpath(path)
os.startfile(path)
Luminiferous answered 4/1, 2018 at 13:22 Comment(2)
Note that startfile is Windows only.Schlock
True, but notice that the OP specifically tagged Windows and Windows-10. For platform independency you can consider this answer.Overshoe
A
19

Other alternatives

import webbrowser, os
path="C:/Users"
webbrowser.open(os.path.realpath(path))

or with os alone

import os
os.system(f'start {os.path.realpath(path)}')

or subprocess

import subprocess,os
subprocess.Popen(f'explorer {os.path.realpath(path)}')

or

subprocess.run(['explorer', os.path.realpath(path)])
Agateware answered 13/12, 2018 at 14:9 Comment(2)
webbrowser also works in Linux; unlike other methods. For subprocess, it will be open, xdg-open or explorer, depending on the OS.Doorway
Already mentioned in this underrated answerOvershoe
E
13

Cross platform:

import webbrowser


path = 'C:/Users'

webbrowser.open('file:///' + path)
Expansible answered 12/2, 2019 at 0:37 Comment(1)
B
0

Windows:

import os
path = r'C:\yourpath'
os.startfile(path)

This method is a simplified version of the approved answer.

Barque answered 18/9, 2021 at 14:15 Comment(0)
O
0

If you want a GUI Try this

import os
from tkinter import *
from tkinter import filedialog

def open_directory():
    directory = filedialog.askdirectory()
    if directory: # if user didn't choose directory tkinter returns ''
        os.chdir(directory) # change current working directory
        for file_name in os.listdir(directory): # for every file in directory
            if os.path.isfile(os.path.join(directory, file_name)): # if it's file add file name to the list
                listbox.insert(END, file_name)

Button(root, text="Choose Directory", command=open_directory).pack()  # create a button which will call open_directory function
Outofdoors answered 30/4 at 6:56 Comment(0)
C
-4
import os

path = "C:\\Users"


def listdir(dir):
    filenames = os.listdir(dir)
    for files in filenames:
        print(files)


listdir(path)

ok here is another piece of cake it list all your files in a directory

Chickadee answered 3/8, 2020 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.