27

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.

1
  • Edit queue is full unfortunately.
    – Cadoiz
    Jul 20, 2021 at 14:11

5 Answers 5

59

I found a simple method.

import os
path = "C:/Users"
path = os.path.realpath(path)
os.startfile(path)
2
  • 3
    Note that startfile is Windows only.
    – 101
    Feb 26, 2019 at 0:16
  • 1
    True, but notice that the OP specifically tagged Windows and Windows-10. For platform independency you can consider this answer.
    – Cadoiz
    Jul 20, 2021 at 14:10
17

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)])
2
  • 3
    webbrowser also works in Linux; unlike other methods. For subprocess, it will be open, xdg-open or explorer, depending on the OS.
    – Polv
    Nov 16, 2020 at 12:50
  • Already mentioned in this underrated answer
    – Cadoiz
    Jul 20, 2021 at 14:08
13

Cross platform:

import webbrowser


path = 'C:/Users'

webbrowser.open('file:///' + path)
1
0

Windows:

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

This method is a simplified version of the approved answer.

-3
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.