各位用户为了找寻关于Python实现遍历目录的方法【测试可用】的资料费劲了很多周折。这里教程网为您整理了关于Python实现遍历目录的方法【测试可用】的相关资料,仅供查阅,以下为您介绍关于Python实现遍历目录的方法【测试可用】的详细内容
本文实例讲述了Python实现遍历目录的方法。分享给大家供大家参考,具体如下:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16# *-* coding=gb2312 *-*
import
os.path
import
shutil
def
traveltree(curPath,count):
if
not
os.path.exists(curPath):
return
if
os.path.isfile(curPath):
fileName
=
os.path.basename(curPath)
print
't'
*
count
+
'├─'
+
fileName
elif
os.path.isdir(curPath):
print
't'
*
count
+
'├─'
+
curPath
pathlist
=
os.listdir(curPath)
for
aa
in
pathlist:
traveltree(curPath
+
""
+
aa,count
+
1
)
if
__name__
=
=
'__main__'
:
traveltree(
"C:py"
,
1
)
运行效果图如下:
希望本文所述对大家Python程序设计有所帮助。