工欲善其事,必先利其器
glob
for subject_folder in glob.glob(os.path.join('/home/cv', '*', '*')):
if os.path.isdir(subject_folder):
# do something
pass
当需要寻找某个目录下的文件(多层目录),尤其好用。
在处理多维数组中,总会出现各种匹配shape的情况,比如[3] -> [3,1],[256,256]->[1,1,256,256],除了增加,还有反过来的情况。
a = np.zeros(shape=(3))
a[:, np.newaxis].shape # (3,1)
b = np.zeros(shape=(256,256))
b[np.newaxis, np.newaxis].shape # (1,1,256,256)
aa = np.squeeze(a) # aa.shape = (3)
b2 = np.squeeze(b) # b2.shape = (256, 256)
b3 = np.squeeze(b, axis=0) # b3.shape=(1,256,256), 消灭第一维(only for dim whose size=1)