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
原文地址:https://www.cnblogs.com/czalinux/p/8184373.html