代码改变世界

python 比较包含相同文件夹

2015-09-02 01:54  小班飞行员  阅读(233)  评论(0)    收藏  举报

import os
import os.path
import sys
import time
import datetime

def match_dir(sDirPath):
    return True
    
def is_datetime_path(sDirPath):
    if not os.path.isdir(sDirPath):
        return False
        
    sDirName = os.path.basename(sDirPath)
    try:
        dt = datetime.datetime.strptime(sDirName,"%Y%m%d")
    except:
        return False
        
    return True      
    
def is_special_path(sDirPath):
    return sDirPath in []

def match_dir(sDirPath):
    
    if not os.path.isdir(sDirPath):
        #print(sDirPath, 1)
        return False
        
    if is_datetime_path(sDirPath): # skip the directory does not contain folio directories
        #print(sDirPath, 2)
        return False
        
    if is_special_path(sDirPath):
        #print(sDirPath, 3)
        return False
        
    #print(sDirPath, 4)
    return True
    
def collect_match_dir(sDirPath, lMatchPath=None):
    if not os.path.isdir(sDirPath):
        return lMatchPath
        
    lMatchPath = [] if lMatchPath is None else lMatchPath
    
    for parent,dirnames,filenames in os.walk(sDirPath):
        for dirname in dirnames:
            
            dirRealPath = parent + os.path.sep + dirname
            if match_dir(dirRealPath):
                lMatchPath.append(dirname)
            elif is_special_path(dirRealPath):
                collect_match_dir(dirRealPath, lMatchPath)

    return lMatchPath    
        
t1 = collect_match_dir('.\\t1')
t2 = collect_match_dir('.\\t2')

lPathCp = []
lPathNcp = []

for s in t1:
    if s in t2:
        lPathCp.append(s)
    else:
        lPathNcp.append(s)
        

print(lPathCp)

print('+++++++++++++++++++++')
print(lPathNcp)

f = open('.\\res.txt', 'w')
for s in lPathCp:
    f.write(s)
    f.write(os.linesep)

f.write('---------------------------------------------')
f.write(os.linesep)

for s in lPathNcp:
    f.write(s)
    f.write(os.linesep)

f.close()