python文件检索模拟find命令

#path代表待搜索的目录路径,result存储搜索到的文件路径列表

#函数将path目录中的全部子目录和文件找到保存至result

def search_dir(path,result):

#使用os中的listdir得到path下的目录和文件,保存到child_files

child_files = os.listdir(path)

for child in child_files:

#通过join函数拼接子目录或文件的路径,存储至child

child = os.path.join(path,child)

#child保存至result

if os.path.isdir(child):#如果child是一个子目录

search_dir(child,result)#调用search_dir继续搜索child

#输入搜索目录和doc文件保存的目录

input_dir = input("输入待搜索的目录:")

output_dir = input("输入保存文件的目录:")

#设置保存子目录与文件路径的列表files

files = list()

#将input中的全部子目录和文件路径找到并保存到files

search_dir(input_dir,files)

for file in files:#遍历files

print("find %s"%(file))#打印搜索到的路径

#如果该路径是一个docx文件

if os.path.isfile(file) and file.endswith('.docx'):

print("move %s"%(file))#打印提示信息

shutil.move(file,output_dir)#将文件移到时output_dir

相关推荐
源码之家6 分钟前
大数据毕业设计汽车推荐系统 Django框架 可视化 协同过滤算法 数据分析 大数据 机器学习(建议收藏)✅
大数据·python·算法·django·汽车·课程设计·美食
HealthScience7 分钟前
COM Surrogate的dllhost.exe高占用(磁盘)解决
python
站大爷IP11 分钟前
用 Python 30 分钟做出自己的记事本
python
曲幽12 分钟前
FastAPI里玩转Redis和数据库的正确姿势,别让异步任务把你坑哭了!
redis·python·mysql·fastapi·web·celery·sqlalchemy·task·backgroundtask
未知鱼18 分钟前
Python安全开发之简易csrf检测工具
python·安全·csrf
何政@25 分钟前
Agent Skills 完全指南:从概念到自定义实践
人工智能·python·大模型·claw·404 not found 罗
AmyLin_200136 分钟前
【pdf2md-3:实现揭秘】福昕PDF SDK Python 开发实战:从逐字符提取到 LR 版面分析
开发语言·python·pdf·sdk·markdown·pdf2md
IP老炮不瞎唠43 分钟前
Scrapy 高效采集:优化方案与指南
网络·爬虫·python·scrapy·安全
沪漂阿龙44 分钟前
深入浅出 Pandas apply():从入门到向量化思维
人工智能·python·pandas
我材不敲代码1 小时前
OpenCV 实战——Python 实现图片人脸检测 + 视频人脸微笑检测
人工智能·python·opencv