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

相关推荐
共享家95272 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr3 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢3 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模3 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn3 小时前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr4 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448914 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu4 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472464 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤4 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板