程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

moving a selected number of images in folder to another folder [duplicate]

发布于2023-02-05 16:21     阅读(698)     评论(0)     点赞(8)     收藏(1)


i have 1200 images in source_folder, i want to copy only 500 images randomly and put them in the destination folder. all the images names are in the list: List = ['1.jpg, '2.jpg', '3.jpg', ...., '1200.jpg']

import glob, random
file_path_type = ["./source_folder/*.jpg"]
images = glob.glob(random.choice(file_path_type))
random_image = random.choice(images)

解决方案


Use "glob" to get the whole list. Shuffle that list, and pick off the first 500. Note that random.shuffle shuffles in place.

import os, glob, random
files = glob.glob("./source_folder/*.jpg")
random.shuffle(files)
for name in files[:500]:
    os.rename( name, "destination_folder")


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1895753/92200dad28a6d0a4f54d/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)