在使用CityScapes数据集的时候,它的训练集里面有18个子文件夹分别是来自18个城市的图片,相应的训练集的标签集里面也有18个子文件夹。我们就要将这些训练图片全部拿出来,所以就用到了文件的递归来得到所有的图片
import os
def recursive_glob(rootdir='.', suffix=''):
"""Performs recursive glob with given suffix and rootdir
:param rootdir is the root directory
:param suffix is the suffix to be searched
"""
return [os.path.join(looproot, filename)
for looproot, _, filenames in os.walk(rootdir)
for filename in filenames if filename.endswith(suffix)]
root='/home/zzp/SSD_ping/my-root-path/My-core-python/DATA/CityScapes'
split="train"
images_base = os.path.join(root, 'leftImg8bit', split)
files[split] = recursive_glob(rootdir=images_base, suffix='.png')
os.walk(rootdir)这个函数讲解如下:
def walk(top, topdown=True, onerror=None, followlinks=False):
"""Directory tree generator.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
dirpath, dirnames, filenames
dirpath is a string, the path to the directory.(是一个str类型)
dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..').(该列表里面显示的是在dirpath目录下的文件夹,如果没有文件夹那就是空列表)
filenames is a list of the names of the non-directory files in dirpath(也就是说filenames里面只能是在dirpath目录下的文件,如果没有文件那么是空列表).
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name).
Example:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end="")
print(sum([getsize(join(root, name)) for name in files]), end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
"""
pass
那么得到的三个参数都是以dirpath为起点的,那么这个参数是怎么变化的呢,才能遍历(gloab)全部的文件?
其实很简单:首先dirpath的值是里面的参数top也就是上面的root,接下来就是一层一层的索引,直到是空列表,转到下一个目录继续索引。详情可以自己复制上面的代码,自己制作一个目录跑一下代码看结果,很容易的看出检索机制。
filename.endswith(suffix)函数讲解如下:
filename是一个str类型的,endswith是这个类型的一个内置函数,当结尾是指定的suffix(后缀)时,返回True;否则返回False。
所以上述代码的目的是:
得到a文件夹里面的所有以.png结尾的文件的完整路径,然后返回一个列表,里面的值就是每个文件的完整路径
import os
a='/home/zzp/SSD_ping/my-root-path/My-core-python/DATA/CityScapes/leftImg8bit'
b=[]
for looproot, _, filenames in os.walk(a):
for filename in filenames:
if filename.endswith('.png'):
b.appand(os.path.join(looproot,filename)