Reputation: 1843
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.
Upvotes: 29
Views: 109125
Reputation: 1
In Ubuntu:
import os
def open_folder():
try:
path = "/home/your_username/Documents"
os.system(f'xdg-open "{path}"')
except Exception as e:
print(f"An error occurred: {e}")
open_folder()
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 195
Windows:
import os
path = r'C:\yourpath'
os.startfile(path)
This method is a simplified version of the approved answer.
Upvotes: 0
Reputation: 15578
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)])
Upvotes: 21
Reputation: 95
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
Upvotes: -4
Reputation: 1843
I found a simple method.
import os
path = "C:/Users"
path = os.path.realpath(path)
os.startfile(path)
Upvotes: 63
Reputation: 131
Cross platform:
import webbrowser
path = 'C:/Users'
webbrowser.open('file:///' + path)
Upvotes: 13