4

I have searched a lot and I know how to open a directory dialog window. But what I am looking for is the method to open a directory folder under windows OS, just like you right click one of your local folder and select open.

Any suggestions?

eyllanesc
  • 221,139
  • 17
  • 121
  • 189
icy
  • 41
  • 1
  • 1
  • 2

6 Answers6

6

Try this:

dir_ = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder:', 'C:\\', QtGui.QFileDialog.ShowDirsOnly)

If the user hits cancel, then dir_ is empty.

eyllanesc
  • 221,139
  • 17
  • 121
  • 189
mont_
  • 303
  • 2
  • 11
  • 1
    Thanks...but you didn't understand my question. These codes open a dialog window for user to choose a dir. However, what I need is just open a system folder under window 7. – icy May 26 '14 at 04:31
5

For python 3.7 you can just do:

os.startfile(path)
mmdfan
  • 306
  • 2
  • 11
  • This is much much older than Python 3.7: [python docs indicates addition in Python 2.0!](https://docs.python.org/2.7/library/os.html#os.startfile). – Joël Feb 19 '21 at 07:37
4

You may simply try this:

os.startfile(whatever_valid_filename)

This starts the default OS application for whatever_valid_filename, meaning Explorer for a folder name, default notepad for a .txt file, etc.

Joël
  • 2,569
  • 17
  • 34
4

For the effect that you are looking for do this :

import os
os.system('explorer.exe "C:\users\%username%\Desktop"')

This opens your Desktop window just like you open any folder. You can substitute C:\users\%username%\Desktop with whatever folder you need to open.

Natesh bhat
  • 10,121
  • 9
  • 68
  • 112
4

To open the folder in a file explorer you can just do this:

import webbrowser


webbrowser.open("path\to\the\file")
 

This works on any platf

3

the answers here are for PyQt4.

So if you try these solutions you will get an error

So to deal with it, here I have the solution for PyQt5

dir_ = QtWidgets.QFileDialog.getExistingDirectory(None, 'Select project folder:', 'F:\\', QtWidgets.QFileDialog.ShowDirsOnly)

And you are done.

Thank me later!

Sohel Shekh
  • 213
  • 2
  • 10