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

相关推荐
檀越剑指大厂3 分钟前
【Python系列】fastapi和flask中的阻塞问题
python·flask·fastapi
YoungHong19921 小时前
【Python进阶】告别繁琐Debug!Loguru一键输出异常日志与变量值
python·debug·异常处理·日志·loguru·log·logger
AiXed2 小时前
PC微信协议之nid算法
python·网络协议·算法·微信
小李哥哥2 小时前
基于数据的人工智能建模流程及源码示例
python
APIshop3 小时前
实战解析:苏宁易购 item_search 按关键字搜索商品API接口
开发语言·chrome·python
蓝桉~MLGT3 小时前
Python学习历程——Python面向对象编程详解
开发语言·python·学习
larance3 小时前
Python 中的 *args 和 **kwargs
开发语言·python
百锦再3 小时前
选择Rust的理由:从内存管理到抛弃抽象
android·java·开发语言·后端·python·rust·go
yaoxin5211233 小时前
238. Java 集合 - 使用 ListIterator 遍历 List 元素
java·python·list
nvd113 小时前
python 后端流式处理 LLM 响应数据详解
开发语言·python