您的位置:首页 > 编程语言 > Python开发

过滤文件代码 python

2017-11-30 16:33 267 查看
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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: