溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

python批量復(fù)制圖片到另一個(gè)文件夾

發(fā)布時(shí)間:2020-09-17 02:58:39 來(lái)源:腳本之家 閱讀:303 作者:lijiao 欄目:開(kāi)發(fā)技術(shù)

本文實(shí)例為大家分享了python批量復(fù)制圖片到文件夾的具體代碼,供大家參考,具體內(nèi)容如下

直接上代碼:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 02 21:03:44 2018
@author: Fsl
"""
import shutil
#這個(gè)庫(kù)復(fù)制文件比較省事
 
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():
 '''
 復(fù)制、重命名、粘貼文件
 :return:
 '''
 local_img_name=r'G:\KeTi\OCT\JPEGImages'
 #指定要復(fù)制的圖片路徑
 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()

就這么多,很簡(jiǎn)單。

小編再為大家分享python實(shí)現(xiàn)圖片批量復(fù)制或刪除的代碼,如下

#coding=utf-8
import os
import shutil 
 
#遞歸復(fù)制文件夾內(nèi)的文件
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): 
      #如果目的路徑不存在該文件就創(chuàng)建空文件,并保持目錄層級(jí)結(jié)構(gòu)
      if not os.path.exists(targetDir): 
        os.makedirs(targetDir) 
      #如果目的路徑里面不存在某個(gè)文件或者存在那個(gè)同名文件但是文件有殘缺,則復(fù)制,否則跳過(guò)
      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)
 
#遍歷某個(gè)目錄及其子目錄下所有文件拷貝到某個(gè)目錄中
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函數(shù)返回一個(gè)三元組
  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")

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI