Python遍历目录以及子目录的文件

 1 #!/usr/bin/env python3
 2 import os
 3 def bbb(dir_str):
 4     filepaths=[]
 5     abspath = os.path.abspath(dir_str)
 6     for x in os.listdir(abspath):
 7         filepath = os.path.join(abspath,x)
 8         if os.path.isdir(filepath):
 9             bbb(filepath)
10         else:
11             print(filepath)
12             filepaths.append(filepath)
13     return filepaths
14 
15 a=bbb('/home/momo/myfile/mydev/python')
16 print('len',len(a))

 

以上代码中有个问题待解决,在11行输出的文件名并未在12行中全部写入list中,需要查一下问题出在哪

posted @ 2017-10-09 17:55  子雅陌曦  阅读(1054)  评论(0编辑  收藏  举报