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


Posted in Python onSeptember 17, 2018

本文实例为大家分享了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 相关文章推荐
Python 解析XML文件
Apr 15 Python
python获取糗百图片代码实例
Dec 18 Python
python3简单实现微信爬虫
Apr 09 Python
Python编程图形库之Pillow使用方法讲解
Dec 28 Python
详解使用python绘制混淆矩阵(confusion_matrix)
Jul 14 Python
Python笔记之工厂模式
Nov 20 Python
使用Python生成200个激活码的实现方法
Nov 22 Python
python爬虫实现获取下一页代码
Mar 13 Python
Python文件时间操作步骤代码详解
Apr 13 Python
使用Python对Dicom文件进行读取与写入的实现
Apr 20 Python
TensorFlow使用Graph的基本操作的实现
Apr 22 Python
Pandas替换及部分替换(replace)实现流程详解
Oct 12 Python
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
Sep 17 #Python
influx+grafana自定义python采集数据和一些坑的总结
Sep 17 #Python
使用 Python 实现简单的 switch/case 语句的方法
Sep 17 #Python
深入理解Python中的 __new__ 和 __init__及区别介绍
Sep 17 #Python
python实现简单http服务器功能
Sep 17 #Python
python实现自动登录
Sep 17 #Python
python发送告警邮件脚本
Sep 17 #Python
You might like
php面向对象中static静态属性与方法的内存位置分析
2015/02/08 PHP
PHP基于文件存储实现缓存的方法
2015/07/20 PHP
PHP版本的选择5.2.17 5.3.27 5.3.28 5.4 5.5兼容性问题分析
2016/04/04 PHP
jQuery contains过滤器实现精确匹配使用方法
2013/04/12 Javascript
Js 代码中,ajax请求地址后加随机数防止浏览器缓存的原因
2013/05/07 Javascript
JavaScript实现从数组中选出和等于固定值的n个数
2014/09/03 Javascript
jQueryMobile之Helloworld与页面切换的方法
2015/02/04 Javascript
JQuery中基础过滤选择器用法实例分析
2015/05/18 Javascript
vue2.0父子组件及非父子组件之间的通信方法
2017/01/21 Javascript
js 原型对象和原型链理解
2017/02/09 Javascript
Web前端框架Angular4.0.0 正式版发布
2017/03/28 Javascript
浅谈js使用in和hasOwnProperty获取对象属性的区别
2017/04/27 Javascript
VUE 自定义组件模板的方法详解
2019/08/30 Javascript
webpack的tree shaking的实现方法
2019/09/18 Javascript
微信小程序 动态修改页面数据及参数传递过程详解
2019/09/27 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
微信小程序实现签字功能
2019/12/23 Javascript
基于jQuery实现挂号平台首页源码
2020/01/06 jQuery
JavaScript中跨域问题的深入理解
2021/03/04 Javascript
[03:28]2014DOTA2国际邀请赛 走近EG战队天才中单Arteezy
2014/07/12 DOTA
[43:41]OG vs Newbee 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.21.mp4
2020/07/19 DOTA
9种python web 程序的部署方式小结
2014/06/30 Python
TensorFlow 合并/连接数组的方法
2018/07/27 Python
python反编译学习之字节码详解
2019/05/19 Python
基于Python实现船舶的MMSI的获取(推荐)
2019/10/21 Python
Django REST 异常处理详解
2020/07/15 Python
python简单实现9宫格图片实例
2020/09/03 Python
Keds官方网站:购买帆布运动鞋和经典皮鞋
2016/11/12 全球购物
皇家道尔顿官网:Royal Doulton
2017/12/06 全球购物
可持续未来的时尚基础:Alternative Apparel
2019/05/06 全球购物
Speedo速比涛法国官方网站:泳衣、泳镜、泳帽、泳裤
2019/07/30 全球购物
《最大的麦穗》教学反思
2014/04/17 职场文书
2014年毕业演讲稿范文
2014/05/13 职场文书
工地标语大全
2014/06/18 职场文书
优秀党员事迹材料
2014/12/18 职场文书
毕业生求职自荐信(2016最新版)
2016/01/28 职场文书