遍历指定目录下的所有文件夹和文件
import os
start_dir = "/Users/finup/Downloads/C"
for cur_path,dirs,files in os.walk(start_dir):
# print("当前查找的目录是%s" % cur_path)
print("CUR_PATH:",cur_path) # 当前查找的路径
print("DIR:",dirs) # 目录下的文件夹
print(files) # 目录下的文件
遍历目录查找文件
import os
keyword = "How Many Wheels.lis"
start_dir = "/Users/finup/Downloads/C"
for cur_path, dirs, files in os.walk(start_dir):
for file in files:
full_path = os.path.join(cur_path, file)
print(full_path)
if file.endswith(keyword) and os.path.getsize(full_path):
print("查找到的文件目录是 %s" % cur_path)
break
原文:https://www.cnblogs.com/zhangmeiyan/p/14298440.html