2017-10-11 47 views
-1

有2個文件夾高分和低分。 Highres文件夾包含大約20個文件夾,裏面有300張圖片。我將高分辨率的圖像轉換爲較小的圖像並複製到低分辨率文件夾現在我想用上面提到的PATH比較從highres到lowres文件夾的丟失圖像。需要Python和Windows。將文件夾和內容與python中的路徑進行比較

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 
+0

請發表您嘗試了代碼。 – quamrana

+0

你有什麼問題?你的「集合」是否包含你期望的數據......? –

+0

忘記在提問時複製和粘貼代碼。請現在檢查 – Murali

回答

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)   
+0

這解決了。非常感謝。 – Murali

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   
+0

謝謝蘇賓。腳本仍然只能用於絕對路徑。它需要檢查子文件夾並打印路徑。 – Murali

相關問題