os.walk 遍历目录

在python中,遍历目录是很方便的,不用自己再去写代码。已经集成在其中了
import os
for root, dirs, files in os.walk('/home/gpadmin1/cxf/gpdata', topdown = True):
    print root,' : ',dirs,' : ',files
root  存放的是所有的目录,递归的
dirs  表示在该目录下还有其他的哪些目录
files 表示在该目录下还有其他的哪些文件
topdown = True 表示前序遍历
topdown = False 表示后续遍历

为清晰起见,只打出root   
>>> for root, dirs, files in os.walk('/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7',topdown = True):
...     print root
...
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10889
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/1
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/1/pgsql_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10890
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10890/pgsql_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_twophase
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_xlog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_xlog/archive_status
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_stat_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_utilitymodedtmredo
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_clog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/global
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_distributedlog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_distributedxidmap
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_log
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact/offsets
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact/members
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_changetracking
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_tblspc
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_subtrans
>>> for root, dirs, files in os.walk('/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7',topdown = False):
...     print root
...
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10889
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/1/pgsql_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/1
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10890/pgsql_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base/10890
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/base
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_twophase
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_xlog/archive_status
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_xlog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_stat_tmp
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_utilitymodedtmredo
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_clog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/global
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_distributedlog
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_distributedxidmap
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_log
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact/offsets
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact/members
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_multixact
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_changetracking
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_tblspc
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7/pg_subtrans
/home/gpadmin1/cxf/gpdata/gpdb_p1/gp7


ps :
森林的两种遍历方法

1.前序遍历森林
    若森林非空,则:
①访问森林中第一棵树的根结点;
②前序遍历第一棵树中根结点的各子树所构成的森林
③前序遍历除第一棵树外其它树构成的森林。

2.后序遍历森林
  若森林非空,则:
①后序遍历森林中第一棵树的根结点的各子树所构成的森林;
②访问第一棵树的根结点;
③后序遍历除第一棵树外其它树构成的森林。
  注意:
     ① 前序遍历森林等同于前序遍历该森林对应的二叉树
     ② 后序遍历森林等同于中序遍历该森林对应的二叉树

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值