Python之文件目录遍历实例代码
2012-12-13 07:59
801 查看
Python之文件目录遍历实例代码
2010-09-25 08:39:30 我来说两句
收藏
我要投稿
Python之文件目录遍历实例代码如下:
import os, sys
from stat import *
def walktree(top, callback):
recursively descend the directory tree rooted at top,
calling the callback function for each regular file
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
Its a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
Its a file, call the callback function
callback(pathname)
else:
Unknown file type, print a message
print Skipping %s % pathname
def visitfile(file):
print visiting, file
if __name__ == __main__:
walktree(sys.argv[1], visitfile)
2010-09-25 08:39:30 我来说两句
收藏
我要投稿
Python之文件目录遍历实例代码如下:
import os, sys
from stat import *
def walktree(top, callback):
recursively descend the directory tree rooted at top,
calling the callback function for each regular file
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
Its a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
Its a file, call the callback function
callback(pathname)
else:
Unknown file type, print a message
print Skipping %s % pathname
def visitfile(file):
print visiting, file
if __name__ == __main__:
walktree(sys.argv[1], visitfile)
相关文章推荐
- Python三种遍历文件目录的方法实例代码
- python文件目录遍历保存成xml文件代码
- [代码实例][Linux系统编程]遍历目录并且输出目录下文件MD5值
- python文件操作之目录遍历实例分析
- Python 判断文件或目录是否存在的实例代码
- python文件操作之目录遍历实例分析
- python 遍历目录(包括子目录)下所有文件的实例
- Python os模块实例之遍历目录及子目录指定扩展名的文件
- Java使用File类遍历目录及文件实例代码
- 遍历文件目录的python 代码
- Lua中遍历文件操作代码实例
- dos下遍历目录和文件的代码(主要利用for命令)
- C++, Python, Matlab, Bash 文件目录遍历
- 【linux c开发】stat opendir目录及文件遍历实例
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
- PHP遍历目录文件中的文件经典代码
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- Python遍历目录和搜索文件中的关键字
- python 遍历目录(包括子目录)下所有文件
- python 遍历目录下的所有文件