遍历目录
Posted sunbinary
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了遍历目录相关的知识,希望对你有一定的参考价值。
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)
以上是关于遍历目录的主要内容,如果未能解决你的问题,请参考以下文章
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段
NC41 最长无重复子数组/NC133链表的奇偶重排/NC116把数字翻译成字符串/NC135 股票交易的最大收益/NC126换钱的最少货币数/NC45实现二叉树先序,中序和后序遍历(递归)(代码片段