python隨機選擇出一些檔案
阿新 • • 發佈:2018-12-05
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檔案,把檔案路徑儲存