比较文件夹和内容与python中的路径

时间:2017-10-11 10:20:26

标签: python

有2个文件夹高位和低位。 Highres文件夹包含大约20个文件夹,其中包含300个图像。我将highres图像转换为较小的图像并复制到lowres文件夹。现在我想比较从提到PATH的highres到lowres文件夹中丢失的图像。需要Python和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 个答案:

答案 0 :(得分:0)

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          

答案 1 :(得分:0)

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)