Python遍历目录

os模块提供了walk方法实现目录遍历

遍历某个目录

import os
path='/root'
for dirpath,dirnames,filenames in os.walk(path):
    for file in filenames:
            fullpath=os.path.join(dirpath,file)
            print fullpath

遍历某个目录下某些后缀的文件

import os
path='/root'
for dirpath,dirnames,filenames in os.walk(path):
    for file in filenames:
            fullpath=os.path.join(dirpath,file)
            if os.path.splitext(fullpath)[1] == '.py':
                print fullpath
posted @ 2018-01-03 16:25  SRE在路上  阅读(284)  评论(0编辑  收藏  举报