python遍历文件

import os
import os.path

rootdir = r"C:\Users\leiyi\Desktop\1" # 指明被遍历的文件夹
#要加上r,否则会报错(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 错误
for parent, dirnames, filenames in os.walk(rootdir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for dirname in dirnames: # 输出文件夹信息
print("parent is:" + parent)

print("dirname is" + dirname)


for filename in filenames: # 输出文件信息
print( "parent is:" + parent)

print("filename is:" + filename)

print( "the full name of the file is:" + os.path.join(parent, filename))
# 输出文件路径信息
posted @ 2019-02-18 16:20  丹心静居  阅读(1386)  评论(0编辑  收藏  举报