【问题标题】:Python Watchdog works on local computer but not on Azure Virtual Machine?Python Watchdog 可以在本地计算机上工作,但不能在 Azure 虚拟机上工作?
【发布时间】:2019-09-08 11:16:37
【问题描述】:

问题: 我正在从 Visual Code Studio powershell 运行这个 python 脚本来检测新文件夹的创建时间。 我在我的本地计算机上试了一下,效果很好。

从这篇文章: https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

唯一的区别是路径: 我的本地计算机 - 在哪里工作:

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

我们的虚拟机在 WINDOWS 2016 数据服务器上不起作用:

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

我们尝试过:

1) 通过 os.path.normpath 规范化路径 - 没有结果。同样的错误

2) 调试它,它说:

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()

这是代码:

# 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()

预期结果:

我们应该让一个脚本永久运行并检查给定路径是否对文件夹和文件结构有任何更改。

【问题讨论】:

  • 是的。这个也试过了。
  • os.path.exists(path) 检查脚本是否可以访问路径的结果是什么?
  • 谢谢。 VM 上存在路径(真),但在启动看门狗脚本时 - 出现相同的错误。 Windows 结构(在本地计算机上运行 Windows 8)与 VM 上的 Windows 数据中心可能存在一些差异吗?
  • 其次 - 家用机器有 64 位,而虚拟机有 32 位 python。这些是我所知道的唯一区别。
  • 抱歉,我无法详细说明此 win 服务器上的行为。我还会测试相对路径是否会改变某些东西(例如path="."),但错误似乎发生在您的代码后面

标签: python azure virtual-machine python-watchdog


【解决方案1】:

我确定您的问题是由 path = "...." 的代码表达式中的转义字符引起的,大多数编程语言如 Python 。请参阅 wiki 页面 Escape character 了解这个概念。

"\U""\t",都是转义字符。如下图,你可以看到它们的真实值。

要修复它,一种解决方案是在表示文件系统路径的字符串值中使用双\字符\\而不是\,另一种是使用/字符而不是\/字符用于POSIX OS的路径字符串,Windows不是POSIX OS,但兼容部分POSIX标准)。

【讨论】:

  • 嗨。实际上不是——然后我们在 Windows 10 上启动了 VM 并安装了 Python 64 位而不是 Datacentre 2016 和 32 位——现在一切都像黄金一样工作!我们拿到了MVP!仍然 - 感谢您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
相关资源
最近更新 更多