64

I was trying to pass a value from one method to another within the class. Because in my code, "if" is calling class's method on_any_event that in return should call my another method dropbox_fn, which make use of the value from on_any_event. Will it work, if the dropbox_fn method is outside the class?

class MyHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        srcpath = event.src_path
        print(srcpath, 'has been ', event.event_type)
        print(datetime.datetime.now())
        # print srcpath.split(' ', 12 );
        filename = srcpath[12:]
        return filename  # I tried to call the method. showed error like not callable

    def dropbox_fn(self):  # Or will it work if this methos is outside the class ?
        # this method uses "filename"

if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    print("entry")
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

The main issue in here is: I cannot call the on_any_event method without event parameter. So rather than returning value, calling dropbox_fn inside on_any_event would be a better way. Can someone help with this?

1

1 Answer 1

100

To call the method, you need to qualify function with self.. In addition to that, if you want to pass a filename, add a filename parameter (or other name you want).

class MyHandler(FileSystemEventHandler):

    def on_any_event(self, event):
        srcpath = event.src_path
        print (srcpath, 'has been ',event.event_type)
        print (datetime.datetime.now())
        filename = srcpath[12:]
        self.dropbox_fn(filename) # <----

    def dropbox_fn(self, filename):  # <-----
        print('In dropbox_fn:', filename)
Sign up to request clarification or add additional context in comments.

2 Comments

So in the "dropbox_fn", I can use the "filename" directly rather than "self.name=filename"?????
ok it worked!!. the "dropbox_fn" successfully got the filename from "on_any_event".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.