python同步两个文件夹下的内容


Posted in Python onAugust 29, 2019

本文实例为大家分享了python同步两个文件夹下的内容,供大家参考,具体内容如下

import os
import shutil
import time
import logging
import filecmp
#日志文件配置
log_filename ='synchro.log'
#日志输出格式化
log_format = '%(filename)s [%(asctime)s] [%(levelname)s] %(message)s'
logging.basicConfig(format=log_format,datefmt='%Y-%m-%d %H:%M:%S %p',level=logging.DEBUG) 
#日志输出到日志文件
fileLogger = logging.getLogger('fileLogger')
fh = logging.FileHandler(log_filename)
fh.setLevel(logging.INFO)
fileLogger.addHandler(fh);
#需要同步的文件夹路径,可以使用绝对路径,也可以使用相对路径
synchroPath1 = r'/home/xxx/image1'
synchroPath2 = r'/home/xxx/image2'

#同步方法
def synchro(synchroPath1,synchroPath2):
 leftDiffList = filecmp.dircmp(synchroPath1,synchroPath2).left_only
 rightDiffList = filecmp.dircmp(synchroPath1,synchroPath2).right_only
 commondirsList =filecmp.dircmp(synchroPath1,synchroPath2).common_dirs
 for item in leftDiffList:
  copyPath = synchroPath1 + '/' + item
  pastePath = synchroPath2 + '/' + item
  if(os.path.isdir(copyPath)):
   copyDir(copyPath,pastePath)
  else :
   shutil.copy2(copyPath,pastePath)
   fileLogger.info('copy '+copyPath +" to "+pastePath)
 for item in rightDiffList:
  copyPath = synchroPath2 + '/' + item
  pastePath = synchroPath1 +'/' + item
  if(os.path.isdir(copyPath)):
   copyDir(copyPath,pastePath)
  else :
   shutil.copy2(copyPath,pastePath)
   fileLogger.info('copy '+copyPath +" to "+pastePath)
 for item in commondirsList:
  copyPath = synchroPath2 + '/' + item
  pastePath = synchroPath1 +'/' + item
  syncDir(copyPath,pastePath)
#拷贝文件夹,如果文件夹不存在创建之后直接拷贝全部,如果文件夹已存在那么就同步文件夹  
def copyDir(copyPath,pastePath):
 if(os.path.exists(pastePath)):
  synchro(copyPath,pastePath)
 else :
  os.mkdir(pastePath)
  shutil.copytree(copyPath,pastePath)
#子文件夹左右两侧文件夹都包含,就同步两侧子文件夹
def syncDir(copyPath,pastePath):
  copyDir(copyPath,pastePath)
  copyDir(pastePath,copyPath)
while(True):
 synchro(synchroPath1,synchroPath2)
 logging.debug('synchro run')
 #阻塞方法,上一步执行结束后等待五秒
 time.sleep(5)

代码简单,但是不优雅,欢迎指正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python入门篇之文件
Oct 20 Python
10款最好的Web开发的 Python 框架
Mar 18 Python
python下paramiko模块实现ssh连接登录Linux服务器
Jun 03 Python
Python操作MySQL数据库的三种方法总结
Jan 30 Python
对numpy和pandas中数组的合并和拆分详解
Apr 11 Python
Django项目中实现使用qq第三方登录功能
Aug 13 Python
将自己的数据集制作成TFRecord格式教程
Feb 17 Python
解决Pycharm 中遇到Unresolved reference 'sklearn'的问题
Jul 13 Python
Python变量格式化输出实现原理解析
Aug 06 Python
python logging模块的使用
Sep 07 Python
python 数据类型强制转换的总结
Jan 25 Python
Python基础之常用库常用方法整理
Apr 30 Python
Python中 CSV格式清洗与转换的实例代码
Aug 29 #Python
详解如何在cmd命令窗口中搭建简单的python开发环境
Aug 29 #Python
python rsync服务器之间文件夹同步脚本
Aug 29 #Python
python-tornado的接口用swagger进行包装的实例
Aug 29 #Python
Python csv模块使用方法代码实例
Aug 29 #Python
python datetime中strptime用法详解
Aug 29 #Python
使用OpenCV实现仿射变换—平移功能
Aug 29 #Python
You might like
PHP代码保护--Zend Guard的使用详解
2013/06/03 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
php中socket通信机制实例详解
2015/01/03 PHP
ThinkPHP5.1框架页面跳转及修改跳转页面模版示例
2019/05/06 PHP
php项目中类的自动加载实例讲解
2019/09/12 PHP
Laravel5.1 框架控制器基础用法实例分析
2020/01/04 PHP
一个js封装的不错的选项卡效果代码
2008/02/15 Javascript
javascript实现禁止右键和F12查看源代码
2014/12/26 Javascript
javascript中new关键字详解
2015/12/14 Javascript
Bootstrap安装环境配置教程分享
2016/05/27 Javascript
vue2组件实现懒加载浅析
2017/03/29 Javascript
JavaScript函数节流的两种写法
2017/04/07 Javascript
AngularJS实现动态添加Option的方法
2017/05/17 Javascript
微信小程序实现点击返回顶层的方法
2017/07/12 Javascript
vue-router 路由基础的详解
2017/10/17 Javascript
js实现删除li标签一行内容
2019/04/16 Javascript
基于Layui自定义模块的使用方法详解
2019/09/14 Javascript
详解简单易懂的 ES6 Iterators 指南和示例
2019/09/24 Javascript
JavaScript 引用类型实例详解【数组、对象、严格模式等】
2020/05/13 Javascript
python中定义结构体的方法
2013/03/04 Python
在Python中操作列表之List.pop()方法的使用
2015/05/21 Python
Python获取linux主机ip的简单实现方法
2016/04/18 Python
selenium python浏览器多窗口处理代码示例
2018/01/15 Python
python使用thrift教程的方法示例
2019/03/21 Python
Python 使用元类type创建类对象常见应用详解
2019/10/17 Python
python对Excel的读取的示例代码
2020/02/14 Python
python使用matplotlib:subplot绘制多个子图的示例
2020/09/24 Python
全球采购的街头服饰和帽子:Urban Excess
2020/10/28 全球购物
年终自我鉴定
2013/10/09 职场文书
应届优秀本科大学毕业生自我鉴定
2014/01/21 职场文书
科技开发中心办公室主任岗位责任制
2014/02/10 职场文书
骨干教师培训方案
2014/05/06 职场文书
青年文明号口号
2014/06/17 职场文书
法院个人总结
2015/03/03 职场文书
圣诞晚会主持词开场白
2015/05/28 职场文书
在职证明书模板
2015/06/15 职场文书