5

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 :)

1 Answer 1

13

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())
Sign up to request clarification or add additional context in comments.

1 Comment

This is the best way to solve the problem. However, if @Vidar wants to run strictly on command line, then he can call os.system("explorer.exe %s" %start_directory)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.