Python随机抽取文件

随机挑选文件夹下文件

 根据序号挑选文件,根据序号挑选json行
 根据文件名挑选文件

Python语法说明

Python 文件读写权限。判断文件目录等方式
   python中对文件/文件夹的操作涉及: os模块 os.path模块 shutil模块。
python操作json文件通过了两种方法:
   load():用于读取json文件;  loads() :将json字符串转换成字典格式; load json string
   dump():用于写入json文件    dumps() 将字典格式数据转换成json格式;
Python 的  random  随机选择元素的方法
   random.random()  random.uniform(1,10) random.randint(1,14) #指定范围,取一个随机整数(顾头顾尾) random.shuffle()
   random.choice(s)
   random.sample()

代码示例

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import os.path
import random
import shutil
import json

def choose_img_num(total_num,choose_num):
    file_path_set= ["/opt/test/dd","/opt/test/ll","/opt/test/pp"]
    file_path = file_path_set[0]
    img_data= os.listdir(file_path)
    random.seed(210)
    choose_file_index = random.sample(range(total_num),choose_num)
    file_path_choose =file_path +"_pick"
    if not os.path.exists(file_path_choose):
        os.mkdir(file_path_choose)
        print(file_path_choose)
    for i,data in enumerate(img_data):
        if i in choose_file_index:
            src_img=  os.path.join(file_path,data)
            dst_img = os.path.join(file_path_choose,data)
            if not os.path.exists(dst_img) or not os.path.isfile(dst_img):
                shutil.copyfile(src_img,dst_img)
    # 提取json-默认每行的对应一个图片
    json_file_path = r"/opt/test/pre/dd/my.json"
    json_des_path = r"/opt/test/my_choose.json"
    choose_file_index = random.sample(range(39),2)
    with open(json_file_path,mode="r",encoding="utf8") as f_r,open(json_des_path,mode="a",encoding="utf8") as f_w:
        for num,data in enumerate(f_r):
            if num in choose_file_index:
                print(num,data)
                f_w.write(data)
    

if __name__== "__main__":
    file_path = "/opt/test/dd"
    img_data= os.listdir(file_path)
    random.seed(210)
    choose_file = random.sample(img_data,2)
    print(choose_file)
    src_json=  r"/opt/test/pre/dd/my.json"
    with open(src_json,'r',encoding='utf8')as fp:
        # 逐行处理
        for num,data in enumerate(fp):
            json_data = json.loads(data)
            if  json_data["image_key"] in choose_file:
                print(json_data["image_key"])
                print(json_data)
原文地址:https://www.cnblogs.com/ytwang/p/15480254.html