python - 使此目录同步脚本检测更改并在后台运行

标签 python python-3.x

我有这个简单的 python 脚本,它将 sourcedir 文件夹的内容同步到 targetdir 文件夹。

这是代码;

from dirsync import sync

sourcedir = "C:/sourcedir"
targetdir ="C:/targetdir"
sync(sourcedir, targetdir, "sync")

每当进行更改时手动运行此脚本很麻烦。我想让这个脚本在后台运行,这样每当 sourcedir 文件夹发生任何变化时,targetdir 文件夹就会自动同步。

我正在使用 python v3.5

最佳答案

一个应用 a library为此:

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


def event_handler(*args, **kwargs):
    print(args, kwargs)


if __name__ == "__main__":
    path = '/tmp/fun'
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

关于python - 使此目录同步脚本检测更改并在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41217151/

相关文章:

python - 如何使用 rtree、python 查找查询框(查询区域)内的最近点

python - Pandas:通过按日期过滤来访问行

python - 类型错误 : 'NoneType' object is not iterable (selenium 2. 49)

python-3.x - Pandas 正则表达式提取两个不同符号前后的所有内容

python - Numpy:乘以一个向量

python - Django migrate : django. db.utils.OperationalError : (1364, "Field ' 名称'没有默认值")

python - 适应 matplotlib 中的协方差等高线绘图示例会引发奇异矩阵错误

python - 当子文件夹具有相同名称时,Airflow Packaged Dags(压缩)会发生冲突

Python:确定一个字符串是否包含数学?

python - python 删除重复项并对列表进行排序