import os
def traverse(py_path
) :
for file in os
.listdir
(py_path
):
fp
=os
.path
.join
(py_path
,file)
if os
.path
.isfile
(fp
):
w
.write
(fp
+'\n')
elif os
.path
.isdir
(fp
):
for newFile
in os
.listdir
(fp
):
newDir
=os
.path
.join
(fp
,newFile
)
if os
.path
.isfile
(newDir
):
w
.write
(fp
+'\n')
if __name__
==__
'main'__
:
w
=open(py_path
+'\A.txt','a')
py_path
=os
.getcwd
()
traverse
(py_path
)
w
.close
()
刚开始学习python,目前只写到遍历进入一层文件夹,往后可通过函数递归来遍历指定路径中所有子文件夹的文件。
转载请注明原文地址: https://lol.8miu.com/read-20966.html