Search code examples
pythonpyqtpyqt5qdesktopservices

PyQt5 | How to open a folder with a file preselected?


Currently I can open a folder by using

dirPath = os.path.dirname(os.path.abspath(self.oVidPath))
QDesktopServices.openUrl(QUrl.fromLocalFile(dirPath))

I want to know if there is anyway I can open folder with a file preselected?

I am okay if it only works on linux systems (nautilus is preferred)

edit : This application is only going be for linux systems


Solution

  • For windows

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.Qt import QProcess
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        command = "explorer /e, /select, c:\\windows\\regedit.exe"
        process = QProcess()
        process.start(command)
        sys.exit(app.exec_())
    

    For Linux

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.Qt import QProcess
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        command = "nautilus /var/log/dpkg.log"
        process = QProcess()
        process.start(command)
        sys.exit(app.exec_())
    

    FYI https://askubuntu.com/a/82717/249546