DDR爱好者之家 Design By 杰米
本文实例为大家分享了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")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2025年11月05日
2025年11月05日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]