import cv2
import numpy as np
import os
import fnmatch
import os
import pandas as pd
import numpy as np
import sys
Addr = 'F:/test/'
AddrSave = 'F:/test/'
isExists=os.path.exists(AddrSave)
if not isExists:
os.makedirs(AddrSave)
AddrSaveTxt = AddrSave+'files.txt'
print(AddrSaveTxt)
import random
def ReadSaveAddr2(FindAddr,SaveAddr,Findtype):
totaln = 0
gList = []
for dirpath,dirnames,filenames in os.walk(FindAddr):
if (dirpath==FindAddr):#自己不用
continue
#print(dirpath)
t = dirpath.split('/')
nowFolder = t[len(t)-1]
t = nowFolder.split('\\')
if(len(t)>1):
nowFolder = t[0] + '/' + t[1]
else:
nowFolder = nowFolder
#for filename in filenames:
a_list = fnmatch.filter(os.listdir(dirpath),Findtype)
for i in range (len(a_list)):
t = nowFolder + '/' + a_list[i]
t = t.split('.')
t = Addr+t[0]+'.jpg'
gList.append(t)
totaln = totaln+1
#打乱
random.shuffle(gList)
print(len(gList))
doc = open(SaveAddr,'w')
for i in range (512):
print(gList[i],file=doc)
doc.close()
print('totaln=',totaln)
ReadSaveAddr2(Addr,AddrSaveTxt,'*.jpg')
运行结果,随机找出512个.jpg文件,把文件路径保存