从某文件夹随机选取5000张图片名

from os import walk
import random

path = '/home/zhouzl/data/BOSSbase_101_256'
random.seed(123456)
for (dirpath, dirnames, filenames) in walk(path):
    indexs = []
    for i in range(len(filenames)):
        index = random.randrange(0, len(filenames))
        if index not in indexs:
            indexs.append(index)
        
        if len(indexs) == 5000:
            break
            
    for i in indexs:
        with open('/home/zhouzl/data/validation.txt', 'a+') as w:
            w.write(filenames[i] + '\n')

你可能感兴趣的:(解决方案)