Python implements folder synchronization

巴扎黑
Release: 2016-12-06 14:04:32
Original
1446 people have browsed it

Define the SynDirTool class, which is used to synchronize the contents of two folders, from the /usr/local/a folder to the /usr/local/b folder. Execution method:

Python code

python SynDirTool.py /usr/local/a  /usr/local/b
Copy after login

SynDirTool.py file Content:

Python code

#!/usr/bin/python  
# -*- coding:utf-8 -*-  
  
import os  
import shutil  
import sys  
import logging  
class SynDirTool:  
    def __init__(self,fromdir,todir):  
        self.fromdir = fromdir  
        self.todir = todir  
  
    def synDir(self):  
        return self.__copyDir(self.fromdir,self.todir)  
  
    def __copyDir(self,fromdir,todir):  
        #防止该目录不存在,创建目录  
        self.__mkdir(todir)  
        count = 0  
        for filename in os.listdir(fromdir):  
            if filename.startswith('.'):  
                continue  
            fromfile = fromdir + os.sep + filename  
            tofile = todir + os.sep + filename  
            if os.path.isdir(fromfile):  
                count += self.__copyDir(fromfile,tofile)  
            else:  
                count += self.__copyFile(fromfile,tofile)  
        return count  
  
    def __copyFile(self,fromfile,tofile):  
        if not os.path.exists(tofile) :  
            shutil.copy2(fromfile,tofile)  
            logging.info("新增%s ==> %s" % (fromfile,tofile))  
            return 1  
        fromstat = os.stat(fromfile)  
        tostat = os.stat(tofile)  
        if fromstat.st_ctime > tostat.st_ctime:  
            shutil.copy2(fromfile,tofile)  
            logging.info("更新%s ==> %s" % (fromfile,tofile))  
            return 1  
        return 0  
  
  
    def __mkdir(self,path):  
        # 去除首位空格  
        path=path.strip()  
        # 去除尾部 \ 符号 或者 /  
        path=path.rstrip(os.sep)  
  
        # 判断路径是否存在  
        isExists=os.path.exists(path)  
  
        # 判断结果  
        if not isExists:  
            # 如果不存在则创建目录  
            logging.info(path+' 目录创建成功')  
            # 创建目录操作函数  
            os.makedirs(path)  
if __name__ == '__main__':  
    srcdir=sys.argv[1]  
    descdir=sys.argv[2]  
    logging.basicConfig(filename='SynDirTool.log', level=logging.INFO)  
    tool = SynDirTool(srcdir,descdir)  
    count += tool.synDir()
Copy after login

Note:

1. The log will be output to the SynDirTool.log file in the same directory as the SynDirTool.py file

2. If the destination folder already has the file, and the file is newer , the files in the source folder will no longer be copied


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template