1、python批量复制图片到文件夹
import shutil
def objFileName():
'''生成文件名列表'''
local_file_name_list = r'G:\KeTi\OCT\ImageSets\Main\test.txt'
obj_name_list = []
for i in open(local_file_name_list,'r'):
obj_name_list.append(i.replace('\n',''))
return obj_name_list
def copy_img():
''' 复制、重命名、粘贴文件 '''
local_img_name=r'G:\KeTi\OCT\JPEGImages'
path = r'G:\KeTi\OCT\data'
for i in objFileName():
new_obj_name = i+'.jpg'
shutil.copy(local_img_name+'/'+new_obj_name,path+'/'+new_obj_name)
if __name__ == '__main__':
copy_img()
2、python实现图片批量复制或删除
import os
import shutil
def copyFiles(sourceDir,targetDir):
if sourceDir.find("exceptionfolder")>0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir,file)
targetFile = os.path.join(targetDir,file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or (os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
print targetFile+" copy succeeded"
if os.path.isdir(sourceFile):
copyFiles(sourceFile, targetFile)
def copyFiles2(srcPath,dstPath):
if not os.path.exists(srcPath):
print "src path not exist!"
if not os.path.exists(dstPath):
os.makedirs(dstPath)
for root,dirs,files in os.walk(srcPath):
for eachfile in files:
shutil.copy(os.path.join(root,eachfile),dstPath)
print eachfile+" copy succeeded"
def removeFileInDir(sourceDir):
for file in os.listdir(sourceDir):
file=os.path.join(sourceDir,file)
if os.path.isfile(file) and file.find(".jpg")>0:
os.remove(file)
print file+" remove succeeded"
if __name__ =="__main__":
copyFiles("./dir1","./dir2")