Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make OS open directory in Python

Tags:

python

I am writing a program in Python, and want to get it to make the OS open the current working directory, making for instance Windows open explorer.exe and navigating to the wanted directory. Any ideas on how to do this?

The directory is already given by os.getcwd.

Cross platform methods preferred :)

like image 498
Vidar Avatar asked May 20 '10 23:05

Vidar


People also ask

How do I open a directory in Python os?

open() method in Python is used to open a specified file path and set various flags according to the specified flags and its mode according to specified mode.

How do I change the os of a directory in Python?

chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path. Parameters: path: A complete path of directory to be changed to new directory path.

What is os Getcwd () in Python?

os. getcwd() returns the absolute path of the current working directory where Python is running as a string str . getcwd stands for "get current working directory", and the Unix command pwd stands for "print working directory". Of course, you can print the current working directory with os. getcwd() and print() .


1 Answers

There is os.startfile, but it's only available under windows:

import os
os.startfile('C:/') # opens explorer at C:\ drive

Here someone (credits to [email protected] apparently) posted an alternative for use on unix-like systems, and someone mentions the desktop package available at pypi (but i've never used it). The suggested method:

import os
import subprocess

def startfile(filename):
  try:
    os.startfile(filename)
  except:
    subprocess.Popen(['xdg-open', filename])

So to complete the answer, use:

startfile(os.getcwd())
like image 126
catchmeifyoutry Avatar answered Oct 03 '22 08:10

catchmeifyoutry