zoukankan      html  css  js  c++  java
  • python 文件夹比较

    参考:http://blog.csdn.net/imzoer/article/details/8675078

    文件比较:filecmp模块:filecmp

    '''
    Created on 2014-6-6
    @author: jyp
    @module: pro1.filecmp_test}
    '''

    import time
    import os
    from site import setBEGINLIBPATH

    def getPrettyTime(state):  
        return time.strftime('%y-%m-%d %H:%M:%S', time.localtime(state.st_mtime))

    def compareDir(A_path, B_path):
        A_files = []
        B_files = []
        for root, dirs, files in os.walk(A_path):
            for file in files:
                A_files.append(root + "\" + file)
        for root, dirs, files in os.walk(B_path):
            for file in files:
                B_files.append(root + "\" + file)
        a_files = []
        b_files = []
        a_path_len = len(A_path)
        b_path_len = len(B_path)
        
        for file in A_files:
            a_files.append(file[a_path_len:])
        for file in B_files:
            b_files.append(file[b_path_len:])
        setA = set(a_files)
        setB = set(b_files)
        commonfiles = setA & setB
        print "#================================="  
        print "common in '", A_path, "' and '", B_path, "'"  
        print "#================================="  
        print ' a: b:'  
        for f in sorted(commonfiles):  
            print f + " " + getPrettyTime(os.stat(A_path + "\" + f)) + " " + getPrettyTime(os.stat(B_path + "\" + f))
                
        onlyA = setA - setB
        print "#================================="  
        print "only in A '", A_path
        print "#================================="  
        print ' a: b:'  
        for f in sorted(onlyA):  
            print f + " " + getPrettyTime(os.stat(A_path + "\" + f))
        onlyB = setB - setA
        print "#================================="  
        print "only in B'" , B_path
        print "#================================="  
        print ' a: b:'  
        for f in sorted(onlyB):  
            print f + " " + getPrettyTime(os.stat(B_path + "\" + f))
    if __name__ == '__main__':
        compareDir('f:/srcDir', 'f:/destDir')
       

  • 相关阅读:
    突袭HTML5之HTML元素扩展(上) 新增加的元素
    DIV常见任务(下) 变身为编辑器
    突袭HTML5之Javascript API扩展3 本地存储
    突袭HTML5之Javascript API扩展5 其他扩展
    DIV常见任务(上) 常规任务
    突袭HTML5之HTML元素扩展(下) 增强的Form元素
    突袭HTML5之WebGL 3D概述(下) 借助类库开发
    突袭HTML5之Javascript API扩展4 拖拽
    突袭HTML5之Javascript API扩展1 Web Worker异步执行
    突袭HTML5之Javascript API扩展2 地理信息服务
  • 原文地址:https://www.cnblogs.com/yongpan666/p/3773712.html
Copyright © 2011-2022 走看看