简体   繁体   中英

Python Watchdog works on local computer but not on Azure Virtual Machine?

THE PROBLEM: I am running this python script from Visual Code Studio powershell to detect when new folder is created. I tried it on on my local computer - it works perfectly.

FROM THIS ARTICLE: https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

The only difference is the path: MY LOCAL COMPUTER - WHERE IT WORKS:

path = "C:\\Users\\Jadzia\\Desktop\\Tests"

OUR VM ON WINDOWS 2016 DATASERVER WHERE IT DOESN'T WORK:

path = "C:\\Users\\tester\\Desktop\\Tests"

WE TRIED:

1) normalizing the path through os.path.normpath - no result. The same error

2) debugging it and it says:

Exception has occurred: OSError
[WinError 123] The filename, directory, name or volume label syntax is incorrect
File "C:\\Users\Tester\Desktop\Tests\start.py", line 67, in <module>
my_observer.start()

THIS IS THE CODE:

# 1) IMPORT SOME STUFF
import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import os



# 2) CREATE AN EVENT HANDLER
if __name__ == "__main__":
    patterns = "*"
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)



# 3) HANDLE ALL THE EVENTS
def on_created(event):
    head, tail = os.path.split(event.src_path)
    if head == path: print('THIS IS IT!!!')

def on_deleted(event): pass
    #print(f"what the f**k! Someone deleted {event.src_path}!")

def on_modified(event): pass
    #print(f"hey buddy, {event.src_path} has been modified")    

def on_moved(event): pass
    #print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")

my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved



# 4) CREATE AN OBSERVER
go_recursively = True
my_observer = Observer()
path = "C:\\Users\Jadzia\Desktop\Tests"
my_observer.schedule(my_event_handler, path, recursive=go_recursively)



# 5) START THE OBSERVER
print("\n")
my_observer.start()           <<<<< HERE IS THE ERROR DURING DEBUGGING
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    my_observer.stop()
    my_observer.join()

EXPECTED RESULT:

We should get a script permanently running and checking for a given path for any changes to folder & file structure.

I'm sure that your issue was caused by the escape character in the code expression of path = "...." with majority programming languages like Python . Please see the wiki page Escape character to know about the concept.

Such as "\\U" or "\\t" , they are escape characters. As below, you can see their real values.

在此处输入图片说明

To fix it, the one solution is to use double \\ characters \\\\ instead of \\ in a string value represented a filesystem path, the other is to use / character instead of \\ (the / character be used in the path string of POSIX OS, Windows is not a POSIX OS, but compatible with partial POSIX standard).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM