Sunday, 2 December 2018

Dynamic directory read and pickle example in python

import math
from sklearn import neighbors
import os
import os.path
import pickle
import glob
from PIL import Image, ImageDraw

nameL = []
name_indx = []
i = 0
X = []
y = []

dirName = glob.glob('data\\*\\')
for name in dirName:
 #print(glob.glob(name+'*.jpg'))
 v = os.path.dirname(name)
 print(v)
 nameL.append(v)
 name_indx.append(i)
 
 i = i+1
 imgs = glob.glob(name+'*.jpg')
 for img_path in imgs:
  print(img_path)
  X.append(img_path)
  
print(nameL)
print(name_indx)
dictionary = dict(zip(name_indx, nameL))
print(dictionary)
#pickling.........
pickle_out = open("dict.pickle","wb")
pickle.dump(dictionary, pickle_out)
pickle_out.close()

#Unpickling.......
pickle_in = open("dict.pickle","rb")
example_dict = pickle.load(pickle_in)
print(example_dict)
print(example_dict[1])

No comments:

Post a Comment