0

我复制了一个示例代码

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()

请帮助我,我是 python 广告看门狗的新手

对于以下问题,我还需要更多答案:以什么形式显示以下代码的输出以及在哪里?如何更改代码中要监控的目录?

并尝试通过将其保存在文件 test.py 中使用以下内容来运行它

python C:\文件夹\test.py

它返回了以下错误

C:\Python33>python c:\folder\test.py
Traceback (most recent call last):
File "c:\folder\test.py", line 4, in <module>
from watchdog.observers import Observer
File "C:\Python33\lib\site-packages\watchdog\observers\__init__.py", line 34,
in <module>
from watchdog.observers.api import BaseObserver, DEFAULT_OBSERVER_TIMEOUT
File "C:\Python33\lib\site-packages\watchdog\observers\api.py", line 62, in <module>
from watchdog.utils.bricks import OrderedSetQueue as SetQueue
File "C:\Python33\lib\site-packages\watchdog\utils\bricks.py", line 112, in <module>
if not sys.version < (2, 6, 0):
TypeError: unorderable types: str() < tuple()
4

1 回答 1

0

这看起来像看门狗中的一个错误。它适用于 Python 2.6 和 2.7。sys.version两者都是字符串,可与元组进行比较。对我来说似乎是一个奇怪的想法。

这种行为可能在 3.x 中有所改变。如果它没有用 3.x 正确测试,你应该会遇到更多的麻烦。使用 Python 2.7 仍然是最安全的,因为许多库还没有提供 3.x 支持。

无论如何,如果你真的,真的想解决这个问题,你可以修补 watchdog\utils\bricks.py 中的第 112 行:

ver = tuple([int(x) for x in sys.version.split()[0].split('.')])
if not ver < (2, 6, 0):

那真的很丑。而是使用 Python 2.7。

于 2013-03-15T09:50:05.070 回答