如何用Python实现目录遍历

Python016

如何用Python实现目录遍历,第1张

1. 基本实现

[root@localhost ~]# cat dirfile.py

import os

path='/tmp'

for dirpath,dirnames,filenames in os.walk(path):

for file in filenames:

fullpath=os.path.join(dirpath,file)

print fullpath

执行结果如下:

[root@localhost ~]# python dirfile.py

/tmp/yum.log

/tmp/pulse-3QSA3BbwpQ49/pid

/tmp/pulse-3QSA3BbwpQ49/native

/tmp/.esd-0/socket

《Python3 教程》

Python3 基础语法

Python3 基本数据类型

Python3 解释器

Python3 注释

Python3 运算符

Python3 数字(Number)

Python3 字符串

Python3 列表

Python3 元组

Python3 字典

Python3 编程第一步

Python3 条件控制

Python3 循环语句

Python3 迭代器与生成器

Python3 函数

Python3 数据结构

Python3 模块

Python3 输入和输出

Python3 File

Python3 OS

Python3 错误和异常

Python3 面向对象

Python3 标准库概览

Python3 实例

Python3 正则表达式

Python3 CGI编程

Python3 MySQL

Python3 网络编程

Python3 SMTP发送邮件

Python3 多线程

Python3 XML解析

Python3 JSON

Python3 日期和时间

Python3 内置函数