'How to get return value from eventhandler of watchdog
I am trying to extract file name using watchdog if there is a newly added txt file in a certain folder destination. When there is a new txt file, it triggers on_created function and return the file name.
# Watchdog API
class EventHandler(FileSystemEventHandler):
def on_created(self, event):
print(event)
fileDir = str(event)
fileName = fileDir.split('\\')
fileName = fileName[len(fileName) - 1]
fileName = fileName[:len(fileName)-2] # remove '>
print(fileName)
return fileName
The problem is that how can I input return value from on_created to my own defined function readfile()?
def readfile(fileName):
# Read Error Msg FileName
filePath=""
if fileName is not None:
filePath = fileName
fhand = open(filePath)
for line in fhand:
line = line.rstrip()
if not line.startswith(r'\\'):
continue
else:
fileDir = line
print("fileDir" + fileDir)
filePathList = fileDir.split('\\')
print(filePathList)
for item in filePathList:
if item.endswith('csv'):
fileName = item
print("fileName" + fileName)
# Read Error Msg Content
fhand2 = open(filePath)
content = fhand2.read()
print(content)
txtcontent = [content, fileName]
return txtcontent
fhand.close()
fhand2.close()
To run the readfile() in main method (missing input at second row),
if __name__ == "__main__":
txtcontent = readfile(#how to input the return value from eventhandler?) # to run the readfile()
path = "\\\\folder\\folder\\folder"
event_handler = EventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
print("observer started")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|