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