Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Opening a folder in Explorer/Nautilus/Finder

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?

like image 969
Ram Rachum Avatar asked Jul 08 '11 22:07

Ram Rachum


People also ask

How do I open a file in Explorer using Python?

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.

How do I find a folder path in Python?

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.


1 Answers

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]) 
like image 194
Cas Avatar answered Sep 22 '22 00:09

Cas