Make OS an open directory in Python

I am writing a program in Python and I want it to force the OS to open the current working directory by creating, for example, Windows open explorer.exe and moving to the desired directory. Any ideas how to do this?

The directory is already defined by os.getcwd.

Preferred cross-platform methods :)

+4
source share
1 answer

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

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

Here, someone (credits Eric_Dexter@msn.com , apparently) posted an alternative for use on unix-like systems, and someone mentions the desktop package available in pypi (but I never used it). 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()) 
+9
source

Source: https://habr.com/ru/post/1310440/


All Articles