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批量创建迅雷任务及创建多个文件
Feb 13 Python
python 3.6 +pyMysql 操作mysql数据库(实例讲解)
Dec 20 Python
Pyqt实现无边框窗口拖动以及窗口大小改变
Apr 19 Python
Numpy array数据的增、删、改、查实例
Jun 04 Python
python框架中flask知识点总结
Aug 17 Python
python设计微型小说网站(基于Django+Bootstrap框架)
Jul 08 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
Aug 04 Python
pytorch 模型可视化的例子
Aug 17 Python
浅谈Python访问MySQL的正确姿势
Jan 07 Python
numba提升python运行速度的实例方法
Jan 25 Python
Python实现给PDF添加水印的方法
Jan 25 Python
详解pycharm的python包opencv(cv2)无代码提示问题的解决
Jan 29 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,const与define的使用区别
2013/06/18 PHP
PHP动态输出JavaScript代码实例
2015/02/12 PHP
Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
2016/03/21 PHP
让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
2009/05/25 Javascript
Jquery工作常用实例 使用AJAX使网页进行异步更新
2011/07/26 Javascript
jquer之ajaxQueue简单实现代码
2011/09/15 Javascript
ListBox实现上移,下移,左移,右移的简单实例
2014/02/13 Javascript
js(JavaScript)实现TAB标签切换效果的简单实例
2014/02/26 Javascript
JQuery中Bind()事件用法分析
2015/05/05 Javascript
Underscore.js 1.3.3 中文注释翻译说明
2015/06/25 Javascript
ES6中如何使用Set和WeakSet
2016/03/10 Javascript
基于Node.js的JavaScript项目构建工具gulp的使用教程
2016/05/20 Javascript
全面理解JavaScript中的继承(必看)
2016/06/16 Javascript
js改变style样式和css样式的简单实例
2016/06/28 Javascript
详解通过JSON数据使用VUE.JS
2017/05/26 Javascript
详解vue项目打包后通过百度的BAE发布到网上的流程
2018/03/05 Javascript
vue实现提示保存后退出的方法
2018/03/15 Javascript
vue+iview 实现可编辑表格的示例代码
2018/10/31 Javascript
支付宝小程序tabbar底部导航
2018/11/06 Javascript
bootstrap table实现iview固定列的效果实例代码详解
2019/09/30 Javascript
vscode 插件开发 + vue的操作方法
2020/06/05 Javascript
JavaScript位置参数实现原理及过程解析
2020/09/14 Javascript
在antd Table中插入可编辑的单元格实例
2020/10/28 Javascript
Python argv用法详解
2016/01/08 Python
使用Python读写及压缩和解压缩文件的示例
2016/07/08 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
Python实现一个带权无回置随机抽选函数的方法
2019/07/24 Python
python 3.7.4 安装 opencv的教程
2019/10/10 Python
pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率
2020/01/02 Python
使用Bazel编译TensorBoard教程
2020/02/15 Python
python 安装impala包步骤
2020/03/28 Python
超级实用的8个Python列表技巧
2020/08/24 Python
英国天然抗衰老护肤品品牌:Nakin Skin Care
2019/04/16 全球购物
关于元旦的广播稿
2014/02/16 职场文书
个人贷款承诺书
2014/03/28 职场文书
关于运动会的广播稿(10篇)
2014/09/12 职场文书