过滤文件代码 python

import os
import cv2
import shutil

# store all file in directory
global totalFileList
totalFileList  = []


def eachFile(filepath):
    pathDir = os.listdir(filepath)
    totalFileList.extend([os.path.join(filepath,filename) for filename in  os.listdir(filepath)])
    print 
    for s in pathDir:
        newDir=os.path.join(filepath,s)     
        if os.path.isfile(newDir) :         
            if os.path.splitext(newDir)[1]==".txt":   
                readFile(newDir)                     
                pass
        else:
            eachFile(newDir)                 

def slectFile(filelist, keyword='jpg', check=True):
    validFileList = []
    validCount = 0
    for fnum, fname in enumerate(filelist):
        fname = fname.strip()
        if not os.path.exists(fname):
            continue 
        if keyword not in os.path.splitext(fname)[-1]:
            continue
        if check:
            img = cv2.imread(fname)
            if None == img:
                continue
            height, width, channel = img.shape
            if (height <= 0) or (width <= 0) or (not channel  == 3):
                continue 
        validCount += 1
        validFileList.append(fname)
    return validFileList

def copyFile(filelist, tgdir):
    if len(filelist) == 0:
        return None
    if not os.path.exists(tgdir):
        os.makedirs(tgdir)
    for file_num, file_name in enumerate(filelist):
        filePath,fileName = os.path.split(file_name)
        newFileName = os.path.join(tgdir, 'skeleton_neg_%08d.jpg'%file_num)
        shutil.copyfile(file_name, newFileName) 
    
if __name__ == "__main__":
   eachFile('./')
   validFileList = slectFile(totalFileList)
   copyFile(validFileList, tgdir='./valid')
   print 'Done'

 

posted on 2017-11-30 16:33  wusichen  阅读(632)  评论(0编辑  收藏  举报