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()