Home > other >  Questions about the watchdog monitoring files dynamically return values
Questions about the watchdog monitoring files dynamically return values

Time:03-25

 
The from watchdog. Observers import Observer
The from watchdog. Events import *

The class FileEventHandler (FileSystemEventHandler) :
# magic method to initialize
Def __init__ (self) :
FileSystemEventHandler. __init__ (self)
Self. FileNameList=[] # file name list (stored filename)


# create a file or folder

Def on_created (self, event) :
If the event. Is_directory:
Print (" directory created: {0} ". The format (event. Src_path))
The else:
Pass
# print (" the file created: {0} ". The format (event. Src_path))
FileAllName=STR (event. Src_path. Split ('/') [1])
If fileAllName. Endswith (' wav ') :
Self. FileNameList. Append (fileAllName)
# self. FileAllName=fileAllName
# print (fileAllName)
Return the self. FileNameList
# print (self. FileNameList)

Def file_path (self, fileNameList) :
FileName=fileNameList
Return the fileName

If __name__=="__main__" :
# to instantiate the Observer object
The observer=the observer ()
Event_handler=FileEventHandler ()
FileNameList=event_handler. FileNameList
B=event_handler. File_path (fileNameList)
For a in b:
Print (a)

# set to monitor directory
Dis_dir="D: \ \ luyin"
IsExists=OS. Path. The exists (dis_dir)
If not isExists:
OS. Makedirs (dis_dir)
The observer. The schedule (event_handler dis_dir, True)
The observer. The start ()
Try:
While True:
# set to monitor frequency (cycle time interval)
Time. Sleep (1)
Except KeyboardInterrupt:
The observer. The stop ()
The observer. The join ()



Now the problem is def on_created () can monitor on the new file normally, need is def file_path () to call def on_created () returns a value, the current has been calling is not successful, please which gives directions, the code above altered, to some mistake, logic is as follows
  • Related