I'm in Python, and I have the path of a certain folder. I want to open it using the default folder explorer for that system. For example, if it's a Windows computer, I want to use Explorer, if it's Linux, I want to use Nautilus or whatever is the default there, if it's Mac, I want to use Finder.
How can I do that?
Creating the File Explorer In order to open a file explorer, we have to use the method, askopenfilename(). This function creates a file dialog object. Parameters: initialdir: We have to specify the path of the folder that is to be opened when the file explorer pops up.
To find out which directory in python you are currently in, use the getcwd() method. Cwd is for current working directory in python. This returns the path of the current python directory as a string in Python.
I am surprised no one has mentioned using xdg-open
for *nix which will work for both files and folders:
import os import platform import subprocess def open_file(path): if platform.system() == "Windows": os.startfile(path) elif platform.system() == "Darwin": subprocess.Popen(["open", path]) else: subprocess.Popen(["xdg-open", path])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With