python批量復制圖片到另一個文件夾
更新時間:2018年09月17日 14:24:25 投稿:lijiao
這篇文章主要為大家詳細介紹了python批量復制圖片到另一個文件夾,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python批量復制圖片到文件夾的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼:
# -*- 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實現(xiàn)圖片批量復制或刪除的代碼,如下
#coding=utf-8
import os
import shutil
#遞歸復制文件夾內(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)建空文件,并保持目錄層級結(jié)構(gòu)
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函數(shù)返回一個三元組
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")
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中將zip壓縮包轉(zhuǎn)為gz.tar的方法
今天小編就為大家分享一篇python中將zip壓縮包轉(zhuǎn)為gz.tar的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
對python條件表達式的四種實現(xiàn)方法小結(jié)
今天小編就為大家分享一篇對python條件表達式的四種實現(xiàn)方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python面向?qū)ο笾蓡T相關(guān)知識總結(jié)
通過面向?qū)ο筮M行編程時,會遇到很多種情況,也會使用不同的成員來實現(xiàn),接下來我們來逐一介紹成員特性和應用場景,需要的朋友可以參考下2021-06-06
Python爬蟲獲取op.gg英雄聯(lián)盟英雄對位勝率的源碼
這篇文章主要介紹了Python爬蟲獲取op.gg英雄聯(lián)盟英雄對位勝率,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Python中tkinter的用戶登錄管理的實現(xiàn)
這篇文章主要介紹了Python中tkinter的用戶登錄管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

