• 大小: 4KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-05-26
  • 语言: Python
  • 标签:

资源简介

本地两个文件夹同步,新建、删除、修改、移动位置、重命名、复制目录或者文件等操作都可以实现,修改文件路径即可用,watchdog思想编写。项目设计涉及到这一块,自己通过watchdog编写了同步的代码,希望可以给大家提供帮助。

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
from watchdog.observers import Observer
from watchdog.events import *
import time
import sys
import os.path 
import shutil
import re

import urllib2
import requests
reload(sys)
sys.setdefaultencoding(‘utf-8‘)
monipath=“/home/fyerd/test2“
syncpath=“/home/fyerd/test3“


class FileEventHandler(FileSystemEventHandler):
    def __init__(self):
        FileSystemEventHandler.__init__(self)

    def on_moved(self event):
        if event.is_directory:
            print(“directory moved from {0} to {1}“.format(event.src_pathevent.dest_path))
            newpath=event.dest_path
            oldpath=event.src_path
            newdir=newpath.replace(monipathsyncpath)
            olddir=oldpath.replace(monipathsyncpath)
            if os.path.exists(olddir):
                os.rename(olddirnewdir)
            else:
                print(“pass“)
        else:
            print(“file moved from {0} to {1}“.format(event.src_pathevent.dest_path))
            a=event.src_path.split(“/“)[-1]
            if re.match(‘.gou‘a[0:4])!=None:#xiugai upload
                xgpath=event.dest_path
                xgdir=xgpath.replace(monipathsyncpath)
                shutil.copyfile(event.dest_pathxgdir)
            else:
                (filepath1filename1) = os.path.split(event.src_path)
                (filepath2filename2) = os.path.split(event.dest_path)
                print(filepath1filepath2)
                # re.match(filepath1filepath2)!=None:#chongmingming and yiwei
                print(“move file!!!!!“)
                newpath=event.dest_path
                oldpath=event.src_path
                print(newpatholdpath)
                newdir=newpath.replace(monipathsyncpath)
                olddir=oldpath.replace(monipathsyncpath)
                print(newdirolddir)
                if os.path.exists(olddir):
                    os.rename(olddirnewdir)
     

评论

共有 条评论

相关资源