PyQt - How to open a directory folder?

前端 未结 6 2054
情歌与酒
情歌与酒 2021-01-19 06:03

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 r

相关标签:
6条回答
  • 2021-01-19 06:08

    For python 3.7 you can just do:

    os.startfile(path)
    
    0 讨论(0)
  • 2021-01-19 06:08

    You may simply try this:

    os.start(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.

    0 讨论(0)
  • 2021-01-19 06:09

    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.

    0 讨论(0)
  • 2021-01-19 06:09

    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

    0 讨论(0)
  • 2021-01-19 06:10

    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!

    0 讨论(0)
  • 2021-01-19 06:18

    Try this:

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

    If the user hits cancel, then dir_ is empty.

    0 讨论(0)
提交回复
热议问题