Search code examples
pythonqtpyqtqfilesystemwatcher

why the program exits after i press Ok


I have written a program that watches over a directory and alerts when a file is added by a user, the file has specific format as user-name.files it works fine but when I press ok of the alert received of new file being added, the program exits, I want it it to stay running.

The below code I have written will run as child process of another PYQT application within that application. So in that I won't be executing main() but will just instantiate SendMyFiles object.

from PyQt4 import QtGui,QtCore
from PyQt4.QtCore import pyqtSlot
import sys
import os

class SendMyfiles(QtGui.QMainWindow):
    def __init__(self):
        super(SendMyfiles, self).__init__()
        self._lookInPath = "/Users/krystosan"
        self.filesList = os.listdir(self._lookInPath)
        print self.filesList
        self.watchMyfilesBin()

    def watchMyfilesBin(self):
        self.fileSysWatcher = QtCore.QFileSystemWatcher()
        self.fileSysWatcher.addPath(self._lookInPath)
        QtCore.QObject.connect(self.fileSysWatcher,QtCore.SIGNAL("directoryChanged(QString)"), self,       
            QtCore.SLOT("slotDirChanged(QString)")) 
        # get list of files as files
        self.newFilesList = os.listdir(self._lookInPath)


    def _connections(self):
        pass

    def recievedfilesFromUser(self):
        newUsrFile =  list(set(os.listdir(self._lookInPath))^set(self.filesList))[0]
        userRecvdFrom = newUsrFile.split(".")[0]
        self.filesList.append(newUsrFile)
        return userRecvdFrom

    @pyqtSlot("QString")   
    def slotDirChanged(self, userfiles):
        userName = self.recievedfilesFromUser()
        retVal = QtGui.QMessageBox.about(self, "Hello %s" % os.getenv('USER'), "Recieved files from %s." % userName)


def main():
    app     = QtGui.QApplication(sys.argv)
    fileSysWatcher  = QtCore.QFileSystemWatcher() 
    window    = SendMyfiles()  
    app.exec_()

if __name__ == '__main__':
    main()

Solution

  • Sorry, I cannot reproduce this one (OpenSUSE 12.3 x64, PyQt 4.9.6).

    I took your code and added a line window.show() to main() (despite your comment saying that you were "doing window.show()"). I also replaced the line

        userRecvdFrom = newUsrFile(".")[0]
    

    with

        userRecvdFrom = newUsrFile.split(".")[0]
    

    The former gives a runtime error because newUsrFile is a string and you can't call it. I also changed the directory being watched as I don't have a directory with that name on my machine.

    After doing this, I could reliably create files in the folder being watched and have the program pop up alert boxes. After each alert was dismissed the program stayed running.

    So I can only speculate at what the problem might be. You say you are instantiating SendMyfiles, in code that you have chosen not to share with us, but how long does this object stay in existence for? Do you keep a reference to this object, or is it only stored in a local variable and hence gets garbage collected at the end of a method? If you have a PyQt window object, and all references to it are lost, Python will garbage-collect it, which will cause the underlying Qt C++ object to be deleted and the window closed.