import os
root_path1 = r'D:\python_code'
file_count = 0
dir_count = 0
def list_files(root_path):
"""
遍历目录
:param root_path:
:return:
"""
global file_count, dir_count
if os.path.isfile(root_path):
print(root_path)
file_count += 1
else:
res = os.listdir(root_path)
for file in res:
full_path = os.path.join(root_path, file)
print(full_path)
if os.path.isfile(full_path):
print(full_path)
file_count += 1
else:
dir_count += 1
list_files(full_path)
def walk_files(root_path):
"""
遍历目录
:param root_path:
:return:
"""
global file_count, dir_count
for root_dir, dirs, files in os.walk(root_path, topdown=True):
for file in files:
print(os.path.join(root_path, file))
file_count += 1
for dir1 in dirs:
print(os.path.join(root_path, dir1))
dir_count += 1
list_files(root_path1)
print(file_count)
print(dir_count)
print("----------------------")
walk_files(root_path1)
print(file_count)
print(dir_count)