Performs recursive(递归) glob(全局) with given suffix and rootdir,使用os.walk(rootdir)和filename.endswith(s
在使用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 topitself, but excluding '.' and '..'), yields a 3-tupledirpath, dirnames, filenamesdirpath 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 indirpath, do os.path.join(dirpath, name).Example:import osfrom os.path import join, getsizefor 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)
Performs recursive(递归) glob(全局) with given suffix and rootdir,使用os.walk(rootdir)和filename.endswith(s相关推荐
- 递归、os.walk、内置函数、lambda、hashlib模块、md5加密、python安装第三方模块、操作mysql数据库...
#递归就是函数自己调自己,一般递归都会有在什么情况下结束递归,一业可以有结束条件#递归最多死循环999次,递归不能设定次数# count=0# def abc():# global count# co ...
- SQL:With recursive 递归用法
With Recursive as 有两种递归字段n的声明写法,第一种是在with- as 中声明需要递归的字段,第二种是在sql语句中第一段的初始值内声明变量. WITH RECURSIVE cte ...
- AngularJS recursive(递归)
工作中我们经常要遍历多层数据,如果数据是已知层级的话,用 ng-repeat 就搞定了,要是数据深度是无限的呢,或者我们要实现一个无限层级的 tree 的时候,该怎么办? 答案是使用 ng-inclu ...
- WITH RECURSIVE递归
1.向上递归查询到父节点(避免死循环) WITH RECURSIVE org AS ( SELECT * FROM tb_org WHERE org_id IN ('**') UNION ALL SE ...
- python~文件遍历命令:glob、os.walk
----补充:定义函数,通常出现在循环结构中. # Description:将一批txt文件的每一段文本保存成独立的文件import os import sysproject_path = os.ge ...
- Python 递归遍历文件夹(递归删除XXX文件夹)慎用
# 批量删除PM1244中CellVideo2 # 请非常谨慎的使用. import shutil import os # PMRootPath = r'X:/PM210603161151244' P ...
- shell快速迁移海量文件
业务需求:需要把一个目录下的1000多万个文件迁移到远程机器 思路:用wget来把文件一个一个的迁移过去,因为文件数量比较大,如果一下在循环操作,会非常慢.所以分批操作,采用化整为零的方法. #! / ...
- python中glob模块怎么下_如何在Python中使用glob.glob模块搜索子文件夹?
如何在Python中使用glob.glob模块搜索子文件夹? 我想在文件夹中打开一系列子文件夹,找到一些文本文件并打印一些文本文件行. 我用这个: configfiles = glob.glob('C ...
- python批量删除文件1001python批量删除文件_Python实现递归遍历文件夹并删除文件...
思路: 遍历文件夹下面的文件夹 如果文件夹名称等于".svn",则修改文件夹的属性(因为".svn"的文件都是只读的,你不能直接删除) 删除此文件夹 如果文件夹 ...
- python递归遍历目录_Python实现递归遍历文件夹并删除文件
思路: 遍历文件夹下面的文件夹 如果文件夹名称等于".svn",则修改文件夹的属性(因为".svn"的文件都是只读的,你不能直接删除) 删除此文件夹 如果文件夹 ...
最新文章
- Oracle查询会话连接数
- Python爬取网站用户手机号_设计师的福利,python爬取素材网站
- Java序列化接口Serializable接口的作用总结
- 爱奇艺体育:体验Serverless极致扩缩容,资源利用率提升40%
- 企业级实战03_真实项目实战SpringMVC整合ActiveMQ
- POJ 1426	Find The Multiple BFS
- Android 渗透测试学习手册 第八章 ARM 利用
- setint 的用法
- EasyUI基础入门之Parser(解析器)
- R语言处理数据——画图时加大标题
- 如何一个月左右拿下PAT甲级(浙大机试)满分
- C/C++利用netsh设置动态IP和静态IP
- 在excel中创建日历
- VPS安装msf教程
- linuxptp分析
- Python入门(二)-编程环境
- 《超级演说家》刘媛媛:寒门贵子
- 从三大方面,分析 to B和 to C产品的区别与联系
- Linux命令整理(二)
- QQ\微信等聊天记录备份与恢复