用python对两个文本进行相同项筛选


        
import os
import os.path as osp

def filter(path):
    file_path = osp.join(path, 'index.txt')
    if osp.exists(file_path):
        return file_path
    index_file = open(file_path, 'a+')
    
    if not os.path.isdir(path):   #判断path是否为路径  
        return   
    for root, dirs, list in os.walk(path):
        for i in list:  
            dir = os.path.join(root, i)    #将分离的部分组成一个路径名  
            #if os.path.getsize(dir) < 60000:   #获取文件大小  
                #os.remove(dir)             #删除文件  
            print (i)
            index_file.write(i+'\n')
    index_file.close()        

def compare(path):
    
    file=osp.join(path, 'label.txt')

    file_path = osp.join(path, 'index.txt')
    
    with open(file_path, 'r') as file1:
         with open(file, 'r') as file2:
             same = set(file1).intersection(file2)

    same.discard('\n')

    with open('some_output_file.txt', 'w') as file_out:
         for line in same:
             file_out.write(line)
 
    file_out.close()

filter(r'D:\Desktop\jiaoben\ci')
compare(r'D:\Desktop\jiaoben\ci')

 
  
保存起来以后备用

你可能感兴趣的:(文本操作)