Compare folders and contents with path in python

Got 2 folders highres and lowres. Highres folder contain about 20 folders with 300 images in it. I convert highres images to smaller and copy to lowres folder. Now I want to compare missing images from highres to lowres folders with PATH mentioned. Need in Python and windows.

    import os
    def get_files(basedir):
      for names, dirs, files in os.walk(basedir):
          for file in files:
               path = os.path.join(names, file)
               yield path [len (basedir)+1:1]
    highres = set(get_files('D:/compare/highres'))
    lowres = set(get_files('D:/compare/lowres'))

    diff_lowres = highres-lowres
    diff_highres = lowres-highres
    print 'Copy to lowres folder :\n' diff_lowres
    print 'Remove extra images from LowRes folder :\n' diff_highres

2 answers

  • answered 2017-10-11 21:05 Subin Gopi

    import os
    
    def get_files(path):
    
        myFiles = []
        files = os.listdir (path)        
        for eachFile in files :        
            if os.path.isfile(os.path.join(path, eachFile)) :
                myFiles.append (eachFile)        
        return   myFiles     
    
    highres = get_files('D:/compare/highres')
    lowres = get_files('D:/compare/lowres')
    
    diff_lowres = []
    diff_highres =[]
    
    for eachLower in lowres :    
        if eachLower not in highres :
            diff_lowres.append (eachLower)
    
    for eachHighres in highres :    
        if eachHighres not in lowres :
            diff_highres.append (eachLower)        
    
    print diff_lowres
    print diff_highres          
    

  • answered 2017-10-12 05:50 Subin Gopi

    import os
    def get_files(path):
    
        myFiles = []
    
        #files = os.listdir (path)
        for names, dirs, files in os.walk(path):            
            for eachFile in files :            
                if os.path.isfile(os.path.join(names, eachFile)) :                    
                    filePath = os.path.abspath(os.path.join(names, eachFile)).replace ('\\', '/')                    
                    finalPath = filePath.replace (path, '')                                                                 
                    myFiles.append (finalPath)    
    
        return myFiles     
    
    loRespath = 'C:/Users/***********/SourceImages/LoRes'
    hiRespath = 'C:/Users/***********/SourceImages/HiRes'
    
    lowres = get_files(loRespath)
    highres = get_files(hiRespath)
    
    diff_lowres = []
    diff_highres =[]
    
    for eachLower in lowres :     
        if eachLower not in highres :
            diff_lowres.append (eachLower)
    
    for eachHighres in highres :    
        if eachHighres not in lowres :
            diff_highres.append (eachLower)