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

相关推荐
勇往直前plus3 分钟前
解决:pycharm运行程序时出现Run ‘python tests for XXX.py‘的问题
ide·python·pycharm
Kratzdisteln5 分钟前
【1902】自适应学习系统 - 完整技术方案
java·python·学习
天若有情6736 分钟前
【Python】从0到1实现轻量级接口测试工具:基于Python+FastAPI+Pytest
python·测试工具·fastapi
weixin_4624462310 分钟前
用 Python Tornado + Vue3 + ECharts 搭建 Docker 实时监控 WebSocket 仪表盘
python·echarts·tornado
ValhallaCoder11 分钟前
Day49-图论
数据结构·python·算法·图论
weixin_4624462314 分钟前
使用 Python + FFmpeg 将 MP4 视频与 SRT 字幕无损合并(支持中文)
python·ffmpeg·音视频
iCan_qi16 分钟前
【游戏开发】一键式图集合并图集分割工具
python·游戏·工具·贴图
小二·18 分钟前
Python Web 开发进阶实战:生物启发计算 —— 在 Flask + Vue 中实现蚁群优化与人工免疫系统
前端·python·flask
名为沙丁鱼的猫72922 分钟前
【万文超详A2A 协议】从个体赋能到群体智能,智能体间的“TCP/IP协议“
人工智能·python·深度学习·机器学习·自然语言处理·nlp
w***765532 分钟前
PHP vs Python:如何选择?
开发语言·python·php