zoukankan      html  css  js  c++  java
  • 过滤文件代码 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'
  • 相关阅读:
    android布局几点随想
    android_handler(一)
    android surfaView surfaHolder video 播放
    java_synchronized 用法
    android_viewFlipper(一)
    android_handler(二)
    解析pdf文档 (lucene3.5)
    Lucene 搜索(小程序)(Lucene3.5)
    运算符重载(++,<<,>>Data类的重载)
    线程池小程序(Java)
  • 原文地址:https://www.cnblogs.com/ruichenduo/p/7929683.html
Copyright © 2011-2022 走看看