python 3.4 - Watchdog on windows with python3.4 -


i trying learn use watchdog utility on windows.

i have gone through basic examples available on google. trying write script monitor given directory , send mail if sub-directory created has file named version.

import time watchdog.observers import observer watchdog.events import dircreatedevent import re import smtplib     class myhandler(dircreatedevent):      def process(self,event):           filetocheck = "version"          open(event.src_path+"\\"+filetocheck) version:             chngstring = version.read()             changenumber = re.findall(r"\d(\d{5})\d",chngstring)             if not changenumber:                 return           server = smtplib.smtp('smtp.gmail.com',587)         server.login("xyz@gmail.com","abc@123")         message = "new build has been create chnage number %d"  %int(changenumber[0])         server.sendmail("xyz@gmail.com","abc@gmail.com",message)      def on_created(self,event):         self.process(event)    if __name__ == '__main__':     observer = observer()     path = "d:\\"     observer.schedule(myhandler(),path,recursive=true)     observer.start()      try:         while true:             time.sleep(1)     except keyboardinterrupt:         observer.stop()      observer.join() 

the problem whenever running script getting error:

>>> ================================ restart   ================================ >>>  traceback (most recent call last): file "c:/users/prince/desktop/keeplearing/watchdog.py", line 37, in <module> observer.schedule(myhandler(),path,path,recursive=true) typeerror: __init__() missing 1 required positional argument: 'src_path' 

i providing proper path while envoking observer.schedule, don't understand why getting this.

please me find out missing.

the error here:

class myhandler(dircreatedevent): 

you've made handler subclass of dircreatedevent. error arising because dircreatedevent's __init__ method, not overriding in myhandler class, has 1 parameter, named src_path, , when create handler using myhandler(), you're not passing in value parameter.

it more correct handler handles events handler is event. instead of subclassing dircreatedevent, need subclass event handler, , filesystemeventhandler seems 1 want.

so, change above line

class myhandler(filesystemeventhandler): 

also, import filesystemeventhandler class watchdog.events.

note myhandler class receive events file creation directory creation. easiest way ignore them modify on_created method to:

    def on_created(self,event):         if isinstance(event, dircreatedevent):             self.process(event) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -