python批量复制图片到另一个文件夹

作者:lijiao 时间:2021-04-30 09:36:50 

本文实例为大家分享了python批量复制图片到文件夹的具体代码,供大家参考,具体内容如下

直接上代码:


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 02 21:03:44 2018
@author: Fsl
"""
import shutil
#这个库复制文件比较省事

def objFileName():
'''
生成文件名列表
:return:
'''
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():
'''
复制、重命名、粘贴文件
:return:
'''
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()

就这么多,很简单。

小编再为大家分享python实现图片批量复制或删除的代码,如下


#coding=utf-8
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)
 #递归遍历文件夹下的文件,用os.walk函数返回一个三元组
 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")
 #removeFileInDir("./dir2")
 #copyFiles2("./dir1","./dir2")

标签:python,复制图片
0
投稿

猜你喜欢

  • pyqt5 使用label控件实时显示时间的实例

    2021-01-29 14:54:17
  • 开发保留标准的浏览器功能的AJAX应用

    2008-02-03 14:03:00
  • 用SQL语句生成带有小计合计的数据集脚本

    2009-01-06 11:33:00
  • 如何用拦截表单的方法上传图片?

    2010-06-16 09:50:00
  • 对python中数组的del,remove,pop区别详解

    2021-01-23 09:22:31
  • python画图时设置分辨率和画布大小的实现(plt.figure())

    2022-11-09 18:12:17
  • asp 过滤尖括号内所有内容的正则代码

    2011-04-03 10:40:00
  • ASP 相关文章或者相关产品

    2011-03-30 11:12:00
  • Opera浏览器简介

    2009-02-05 20:56:00
  • ASP使用wsImage组件给图片加水印代码

    2010-06-09 19:23:00
  • 浅析python 中大括号中括号小括号的区分

    2021-06-15 15:14:16
  • Python使用scapy模块发包收包

    2021-04-26 16:32:12
  • 解决pycharm同一目录下无法import其他文件

    2021-03-22 10:02:14
  • ASP字符串大写转换成小写 ASP小写转换成大写 ucase lcase

    2011-03-31 10:59:00
  • Python爬取酷狗MP3音频的步骤

    2022-04-07 03:15:21
  • python提取字典key列表的方法

    2023-02-01 23:35:28
  • 层叠加的五条叠加法则

    2009-05-01 12:07:00
  • Python绘图Turtle库的安装问题解决

    2022-06-08 03:44:22
  • 用户研究中的人物角色

    2010-02-08 12:15:00
  • 什么是python的必选参数

    2022-12-04 07:28:36
  • asp之家 网络编程 m.aspxhome.com