os.walk|图片数据集

该函数的功能:遍历指定文件夹下的所有【路径】【文件夹】【文件名】

'''
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
参数:
top -- 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。
root 所指的是当前正在遍历的这个文件夹的本身的地址
dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历top文件夹,与top 文件夹中每一个子目录。
onerror -- 可选,需要一个 callable 对象,当 walk 需要异常时,会调用。
followlinks -- 可选,如果为 True,则会遍历目录下的快捷方式(linux 下是软连接 symbolic link )实际所指的目录(默认关闭),如果为 False,则优先遍历 top 的子目录。
'''
函数定义
#查看root的所有值【root代表当前遍历文件夹的路径】
for root,dirs,files in os.walk(".",topdown=True):
    print(os.getcwd())
    print(root)

'''
说明:topdown = True  从最上层开始遍历  得到当前文件夹下的所有文件夹

返回结果:

D:\python\TensorFlow\1_data_input_create\4.3    ##1.当前工作目录一直没有改变(脚本所在目录)
.                                                   ##遍历顶层文件夹【'.'代表当前工作目录【一层】】
D:\python\TensorFlow\1_data_input_create\4.3    ##1.当前工作目录一直没有改变(脚本所在目录)
.\mnist_digits_images                               ##遍历到子文件【二层】
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\0                             ##遍历到子文件夹【三层】,【三层】有10个文件夹,一次遍历
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\1
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\2
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\3
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\4
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\5
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\6
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\7
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\8
D:\python\TensorFlow\1_data_input_create\4.3
.\mnist_digits_images\9

'''
查看所有root
for root,dirs,files in os.walk(".",topdown=True):
    print(dirs)
#
'''
['mnist_digits_images']                              ###指定目录下,只有一个文件夹【二层】
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']   ###正在遍历的文件夹有10个文件夹【三层】
[]                                                   ### 文件夹0 中没有文件夹
[]                                                   ### 文件夹1 中没有文件夹
[]
[]
[]
[]
[]
[]
[]
[]
'''
查看所有dirs
for root,dirs,files in os.walk(".",topdown=True):
    print(files)

'''
['4.3_data_input_create.py', 'os模块.py', '配套知识点.py']    ###【一层】所有文件  
[]                                                                                  ###【二层】没有文件
['0.bmp', '1.bmp', '10.bmp', '100.bmp', '101.bmp', '102.bmp', '103.bmp', '104.bmp',
                              ###【三层】文件较多,只列举了文件夹0中的文件,文件夹1的文件类似
查看所有files
##文件名和路径组合成文件名【绝对路径】,路径分离出当前文件夹名
for (dirpath,dirsname,filesname) in os.walk('mnist_digits_images',topdown=True):
    for filename in filesname:
        filename_path = os.sep.join([dirpath,filename])
        print(filename_path)
        time.sleep(1)
        dir_name = dirpath.split('\\')[-1]
        print(dir_name)
        time.sleep(12)
        '''
        第一次循环
        mnist_digits_images\0\0.bmp   ##文件的绝对路径
        0                             ##当前文件名
        第二次循环
        mnist_digits_images\0\1.bmp
        0
        '''
文件绝对路径和提取遍历位置的文件名

猜你喜欢

转载自www.cnblogs.com/liuhuacai/p/11552670.html