2016-12-02 24 views
1

我公司所擁有的文件名爲蟒蛇 - 找到的文件名不同於FOLDERNAME

"3688-35(DUSTY GREY-BLK).jpg" 
"3688-36A(SLIVER).jpg" 
"..." 

應放置在「」文件夾中。

經過長年累月我現在面臨6位數字圖片和〜3500個文件夾無數放錯地方的圖片需要進行檢查放置在正確的文件夾,

所以我覺得我可以寫一個腳本只能列出錯誤放置的文件和文件夾的像

"1111/1112.jpg" 
"1234/1243.jpg" 

我做一些搜索後,我發現match filenames to foldernames then move files是什麼樣的,我需要,但我不能修改回答我的需要,因爲文件名模式。

我一開始堅持,但我認爲https://pymotw.com/2/glob/可以做一些上市和詭計https://linux.die.net/man/3/fnmatch

回答

0

我解決了我的問題。

from glob import glob 
import logging, time, os 

def listdirs(path): 
    return [d for d in os.listdir(path) if os.path.isdir(d)] 

def find_pics(): 
    folders = listdirs(".") 
    for dir in folders: 
     time.sleep(0.3) 
     pics = os.listdir(dir) 
     for pic in pics: 
      if pic.endswith(".jpg"): 
       if dir not in pic: 
        logging.warning(dir + '/' + pic) 
       else: 
        pass 
      else: 
       pass 
    return 

def main(): 
    logging.basicConfig(filename='wrong_placed.log', filemode='w', level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s \n') 
    logging.debug("Starting to check is the picture wrong placed.") 
    find_pics() 
    return 

if __name__ == '__main__': 
    main()