python:文件目录遍历器

Posted 流星曳尾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python:文件目录遍历器相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import json

file = open(‘a.txt‘,‘w‘)
for root,dirs,files in os.walk(‘./‘):
file.write(‘root:‘ + root + ‘ ‘)
file.write(‘dirs:‘ + json.dumps(dirs) + ‘ ‘)
#json.dumps(object) 编码(把object变成json型string) json.loads(string) 解码
file.write(‘files:‘ + json.dumps(files) + ‘ ‘)
file.write(‘ ‘)
file.close()

以上是关于python:文件目录遍历器的主要内容,如果未能解决你的问题,请参考以下文章