在使用jupyter的过程中,我们有时候写的一些代码片段因为忘记存在哪个ipynb文件下,因此很难找到
所以我写了一个程序:
import os
root_path=input("查找根路径")#windows 示例:d:/do/home
key_desc=input("查找关键代码字段")#示例:recall
result=[]
file_list=[root_path]
while file_list:
head=file_list.pop(0)
for sub_node in os.listdir(head):
#是否是目录
if os.path.isdir(head+"/"+sub_node):
file_list.append(head+"/"+sub_node)
#是否是ipynb文件
if sub_node.endswith(".ipynb"):
with open(head+"/"+sub_node,"r",encoding="utf-8") as file:
txt=file.read()
if key_desc in txt:
result.append(head+"/"+sub_node)
print(result)
整段代码遵循广度遍历的思想,从根目录开始访问子目录,一层一层遍历,
直到整个节点队列为空
运行结果如下: