python shutil复制文件夹内所有内容
接到测试的需求跟图片数量限制相关,所以写个脚本来方便工作
# -*- coding: utf-8 -*-
# @Time : 2021/9/24
# @File : setnewjpg.py
# @Software: PyCharm
# 用于复制生成大量的图片
import shutil
import os
import random
listpath =[]
listd = []
# (复制路径,复制数量,复制的名称,复制后地址)
def setjpgfile(path,numjpg, name, saveadress):
# 获取文件夹下所有文件的路径,os.walk() 方法用于通过在目录树中游走输出在目录中的文件名
for root, dirs, files in os.walk(path):
for file in files:
# 根据文件名,拼接完整文件名
adir = os.path.join(root, file)
listd.append(adir.replace('\\', '/'))
# 根据原图片进行批量复制
for i in range(len(listd)):
copyjpg(numjpg, name, listd[i], saveadress)
# (复制数量,复制的名称,原地址,复制后地址),重复复制文件内容
def copyjpg(numjpg,name,addressjpg,saveadress):
for a in range(0,numjpg):
randomnum = random.randint(1, 10000000000)
shutil.copyfile(addressjpg, saveadress+name+str(randomnum)+"KW.jpg")
if __name__ == '__main__':
# copyjpg(100,"abab","D://testjpg/600.jpg","D://testjpg/")
path = "D://testjpg//test5" # 文件夹路径
path2 = "D://testjpg//test10"
setjpgfile(path, 90, "KWQS", "D://testjpg/big500/")
setjpgfile(path2, 90, "KWQS", "D://testjpg/big1000/")