python同步文件

import collections
import os
import re
import shutil
import sys
def file_sync(src_path,dest_path):
    if src_path==dest_path or not os.path.isdir(src_path) or not os.path.isdir(dest_path):
        return
    dir_pa=re.compile(r"target|.mvn|.git")
    file_pa=re.compile(r"mvnw|.DS_Store|.gitignore|.+iml|.+log")
    queue_src_path=collections.deque()
    queue_dest_path=collections.deque()
    temp_path=r"D:\threadDumps"
    temp_tiple=os.walk(temp_path)
    temp_next=next(temp_tiple)
    temp_dirs=temp_next[1]
    temp_files=temp_next[2]
    queue_src_path.append(src_path)
    queue_dest_path.append(dest_path)
    while len(queue_src_path)>=0 and len(queue_dest_path)>=0:
        if len(queue_src_path)==0 and len(queue_dest_path)==0:
            queue_src_path.append(src_path)
            queue_dest_path.append(dest_path)
        src_path=queue_src_path.popleft()
        dest_path=queue_dest_path.popleft()
        src_tiple=os.walk(src_path)
        src_next=next(src_tiple)
        src_path=src_next[0]
        src_dirs=src_next[1]
        src_dirs=[val for val in src_dirs if not dir_pa.match(val)]
        src_files=src_next[2]
        src_files=[val for val in src_files if not file_pa.match(val)]
        if(os.path.isdir(dest_path)):
            dest_tiple=os.walk(dest_path)
            dest_next=next(dest_tiple)
            dest_path=dest_next[0]
            dest_dirs=dest_next[1]
            dest_files=dest_next[2]
        src_files=[val for val in src_files if not val.endswith("iml") or val.endswith("log")]
        if len(dest_files)>0:
            temp_files=[val for val in temp_files if val not in dest_files or val not in temp_files]
            src_files=[val for val in src_files if val not in dest_files]
        os.chdir(temp_path)
        i=j=0
        while i<len(temp_files) and j<len(src_files):
            temp_file=temp_files[i]
            src_file=src_files[j]
            shutil.copyfile(src_path+os.path.sep + src_file,temp_file)
            if(temp_file not in temp_files or temp_file not in dest_files):
                os.rename(temp_file,src_file)
            if(os.path.isdir(dest_path)):
                if(os.path.isfile(src_file)):
                    shutil.move(src_file,dest_path)
                if(os.path.isfile(temp_file)):
                    shutil.move(temp_file,dest_path)
            i+=1
            j+=1
        i=j=0
        if(len(dest_dirs)>0):
            temp_dirs=[val for val in temp_dirs if val not in dest_dirs or val not in temp_dirs]
            src_dirs=[val for val in src_dirs if val not in dest_dirs]
        while i<len(temp_dirs) and j<len(src_dirs):
            temp_dir=temp_dirs[i]
            src_dir=src_dirs[j]
            if(os.path.isdir(temp_dir) and temp_dir not in dest_dirs):
                os.rename(temp_dir,src_dir)
            if(os.path.isdir(src_dir) and src_dir not in dest_dirs):
                shutil.move(src_dir,dest_path)
            elif(os.path.isdir(temp_dir) and temp_dir not in dest_dirs):
                shutil.move(temp_dir,dest_path)
            i+=1
            j+=1
        for dest_dir in dest_dirs:
            queue_src_path.append(src_path+os.path.sep+dest_dir)
            queue_dest_path.append(dest_path+os.path.sep+dest_dir)
src_paths=[]
dest_paths=[]
def same_files(src_path,dest_path):
    if src_path==dest_path or not os.path.isdir(src_path) or not os.path.isdir(dest_path):
        return
    src_tiple=os.walk(src_path)
    src_next=next(src_tiple)
    src_path=src_next[0]
    src_dirs=src_next[1]
    src_files=src_next[2]
    dest_tiple=os.walk(dest_path)
    dest_next=next(dest_tiple)
    dest_path=dest_next[0]
    dest_dirs=dest_next[1]
    dest_files=dest_next[2]
    i=len(src_dirs)-1
    j=len(dest_dirs)-1
    if(len(src_files)==len(dest_files) and i==j):
        while i>=0 and j>=0:
            i-=1
            j-=1
            same_files(os.path.join(src_path,src_dirs[i]),os.path.join(dest_path,src_dirs[j]))
    else:
         src_paths.append(src_path)
         dest_paths.append(dest_path)
def move_file_path(file,dir_path):
    if not os.path.isfile(file) or not os.path.isdir(dir_path):return
    path = r"D:\threadDumps"
    os.chdir(path)
    temp_tiple = os.walk(path)
    temp_next = next(temp_tiple)
    temp_dirs=temp_next[1]
    while(not os.path.isdir(dir_path)):
        base_name=os.path.basename(dir_path)
        dir_name=os.path.dirname(dir_path)
        os.renames(temp_dirs[0],base_name)
        shutil.move(base_name,dir_name)
        dir_path=dir_name+os.path.sep+base_name
    temp_files = temp_next[2]
    temp_files=[val for val in temp_files]
    src_len = len(temp_files)
    if (src_len == 0): return
    src_file = os.path.basename(file)
    if(not os.path.isfile(dir_path+os.path.sep + src_file)):
        shutil.move(temp_files[0],dir_path)
        os.chdir(dir_path)
        os.rename(temp_files[0], src_file)
    shutil.copyfile(file, dir_path + os.path.sep + src_file)
for i in range(1,len(sys.argv),2):
    same_files(sys.argv[i],sys.argv[i+1])
i=len(src_paths)-1
j=len(dest_paths)-1
if len(src_paths)==len(dest_paths):
    while len(src_paths)==len(dest_paths) and len(src_paths)>0 and len(dest_paths)>0:
        print(src_paths)
        print(dest_paths)
        src_path=src_paths.pop()
        dest_path=dest_paths.pop()
        file_sync(src_path,dest_path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

car-java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值