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

'''
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
'''

文件绝对路径和提取遍历位置的文件名

##将字符串类型的文件名称,映射成数字类型
##去重排序:set()--无序不重复集合类型 sorted()排序,默认升序 list()变成列表形式
lab = list(sorted(set(labelsnames)))
'''
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
'''
##映射成数字
labdict = dict(zip(lab,list(range(len(lab)))))
'''
{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} 补充:
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
''' labels = [labdict[i] for i in labelsnames]
'''
列表解析:通过遍历所有字符串类型的文件夹名称【'0','0',````['9']】,通过字典取值,获得数字类型的文件名[0,0,`````,9]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ``````9]每个数字都对应一张图片
'''

将字符串类型的文件名称,映射成数字类型

a = np.array(labelsnames)
'''
将列表形式转化成数组形式
['0' '0' '0' ... '9' '9' '9']
'''
b = shuffle(np.asarray(lfilenames),np.asarray(labels))
'''
from sklearn.utils import shuffle 乱序
[array(['mnist_digits_images\\8\\292.bmp',
'mnist_digits_images\\1\\668.bmp',
'mnist_digits_images\\6\\121.bmp', ...,
'mnist_digits_images\\7\\821.bmp',
'mnist_digits_images\\6\\308.bmp',
'mnist_digits_images\\7\\286.bmp'], dtype='<U29'), array([8, 1, 6, ..., 7, 6, 7])] '''

转换成数组,并且乱序

os.walk|图片数据集的更多相关文章

  1. DCGAN增强图片数据集

    DCGAN增强图片数据集 1.Dependencies Python 3.6+ PyTorch 0.4.0 numpy 1.14.1, matplotlib 2.2.2, scipy 1.1.0 im ...

  2. python os.walk()

    os.walk()返回三个参数:os.walk(dirpath,dirnames,filenames) for dirpath,dirnames,filenames in os.walk(): 返回d ...

  3. os.walk()

    os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. walk()方法语法格式如下: os.walk(top[, topdown=True[, onerror=None[ ...

  4. [py]os.walk爬目录&sys.argv灵活获取参数

    1, 遍历目录 os.walk('/tmp') os.next()   2,sys.argv ######################################## py@lanny:~/t ...

  5. Python 用 os.walk 遍历目录

    今天第一次进行 文件遍历,自己递归写的时候还调试了好久,(主要因为分隔符号的问题),后来发现了os.walk方法,就忍不住和大家分享下. 先看下代码: import os for i in os.wa ...

  6. python os.walk()和os.path.walk()

    一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...

  7. python 简单示例说明os.walk和os.path.walk的不同

    import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,fi ...

  8. os.walk获取同级目录具有随机性

    1.在不同机器上,相同内容的目录和文件,os.walk获取结果中路径的先后顺序具有随机性. 2.查看os.walk源码得知,listdir具有随机性. 3.修改该源码,对listdir结果排序后,使得 ...

  9. python os.walk()遍历

    os.walk()遍历 import os p='/bin' #设定一个路径 for i in os.walk(p): #返回一个元组 print (i) # i[0]是路径 i[1]是文件夹 i[2 ...

随机推荐

  1. Linux操作系统(四)_部署MySQL

    一.部署过程 1.当前服务器的内核版本和发行版本 cat /etc/issue uname -a 2.检查系统有没有自带mysql,并卸载自带版本 yum list installed | grep ...

  2. 关系型数据库MySQL(三)_触发器

    简介 用来给保证数据完整性的一种方法,经常用于加强数据的完整性: 是与表事件相关的特殊的存储过程,与存储过程的唯一区别是触发器不能执行execute语句调用,而是在用户执行SQL语句时自动触发执行 执 ...

  3. select的限制与poll的使用

    select的限制 select的并发数受到两个限制:1.一个进程能打开的最大描述符数量;2.select中fd_set集合容量的限制(FD_SETSIZE) 关于进程的最大描述符数量: ulimit ...

  4. 爬取拉勾网所有python职位并保存到excel表格 对象方式

    # 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...

  5. centOS发布.Net Core 2.0 API

    1.dotnet  xxx.dll & & 放在启动参数后面表示设置此进程为后台进程.(目前测试无效) 2.ps -ef | grep xxx ps:将某个进程显示出来 -A 显示所有 ...

  6. SpringBoot入门简介

    SpringBoot诞生的背景 所有软件行业里面,如果要说商用体系,排在第一位的永远是java,因为java的体系丰富,支持度高,安全性也高 但是我们所有的开发者也不得不去忍受Java中的以下痛苦 举 ...

  7. elasticsearch 嵌套对象之嵌套类型

    nested类型是一种特殊的对象object数据类型(specialised version of the object datatype ),允许对象数组彼此独立地进行索引和查询. 1. 对象数组如 ...

  8. 403 ,502 到正确的nginx 配置

    配置完一定要reboot ,之前我一直用的 ./nginx -s reload ,这次我不知道为啥不行... 再没有reboot 之前一直在用的旧的配置.所以一直在报403forbbdin. rebo ...

  9. 最大公因数数gcd模板

    首先蒟蒻是在大佬的博客里学习的代码,代码风格多有相似之处,大佬博客https://www.cnblogs.com/lMonster81/p/10433902.html 最大公因数那,顾名思义就是两个数 ...

  10. JavaScript常用技巧之进制转换

    一.十进制转二进制 (8).toString(2) 二.二进制转十进制 parseInt("1000",2) 三.获取当前时间戳 // 方法 Date.now() // 对象和操作 ...