首页 > 编程语言 > 详细

Python OS操作,查找文件

时间:2021-01-19 19:30:19      阅读:27      评论:0      收藏:0      [点我收藏+]
遍历指定目录下的所有文件夹和文件

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

Python OS操作,查找文件

原文:https://www.cnblogs.com/zhangmeiyan/p/14298440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!