当前位置:网站首页>如何在电脑成千上万支文件中找到想要的文件?
如何在电脑成千上万支文件中找到想要的文件?
2022-06-21 18:42:00 【极简小课】
本篇文章主要介绍利用Python写段小程序,来快速找到自己想要的文件。
废话不多说,先上代码,然后再解释。
import os
def findfiles(name, path):
# 定义保存查找结果的数组
search_result = []
# 利用os.walk函数遍历路径下的所有文件
for dir_path, dir_names, filenames in os.walk(path):
for filename in filenames:
if name in filename:
search_result.append(os.path.join(dir_path, filename))
# enumerate函数会以list的形式返回列表的下标和值
for index, value in enumerate(search_result):
# 在查找结果里,按照序号、文件名分行打印输出
print("[" + str(index+1) + ",", "'" + value + "'" + "]")
if __name__ == '__main__':
search_path = input("请输入要搜索的路径:")
file_name = input("请输入要查找的文件名字:")
findfiles(file_name, search_path)这段代码其实很简单,主要涉及两个主要的函数,一个是os模块的walk函数,一个是内置函数enumerate。
1. 我们先来看下walk函数,os.walk函数就是遍历所有文件,什么意思呢?就是如果你当前搜索的路径下还有文件夹,会继续进入到这个文件夹下查找文件,一层一层找下去,直到最后没有文件夹。
walk函数返回三个值:
dir_path: 当前搜索路径
dir_names: 当前路径下存在的所有文件夹
filenames: 当前路径下存在的所有文件
比如下面例子,在文件夹"100days"下面,有个"day01"文件夹,但是没有文件, 所以第三个值是空的列表,然后会进入"day01"文件夹继续找文件,发现已经没有文件夹了,只有两支文件。
F:\Study\Python\100days ['day01'] []
F:\Study\Python\100days\day01 [] ['flag.py', 'peppa_pig.py']2. enumerate函数是python内置函数,用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
什么意思呢?就是在for循环里用enumerate作用一个可遍历的对象,不但会返回对象里的每个值,还会返回值对应的下标,有时候我们需要用到值对应的下标,所以就会使用enumerate函数,写段小代码演示下
names = ["张三", "李四", "小明", "大名"]
for index, name in enumerate(names):
print(index, name)输出结果:
0 张三
1 李四
2 小明
3 大名这样我们就知道张三在第一个位置,李四在第二个位置等等。
最后来看下总程序执行结果:
比如在电脑的F:\Study\Python路径下查找名字包含day的文件,结果如下:
扩展:
os模块下还有一个函数也是用来查找文件,就是os.listdir,和os.walk的区别就是,os.listdir只会返回当前路径下的所有文件夹和所有文件,不会继续再深入下一层文件夹继续查找。
边栏推荐
- Excuse me, the exclusive resources in data integration can not connect to some databases normally. The following reasons do not seem to be true. Public funds
- 汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计
- 日常开发常用工具提升效率
- 简单使用JS
- A simple architecture design logic | acquisition technology
- 研报精选 | 麦肯锡《中国数字创新未来》趋势解读
- Model evaluation and selection of machine learning
- Post to asp Net core length limitation and solution when transferring data
- Grain College p40~43
- Software testing office tools recommendation - Desktop Calendar
猜你喜欢

瀚高数据库自定义操作符'!~~'

ENVI-Classic-Annotation-object添加的元素图例比例尺如何撤回修改删除

Inno setup change installation path learning
![[cvpr2022] CMU tutorial on multimodal machine learning, 200+ pages to explain the knowledge of multimodal learning system with six challenges of representation, alignment, reasoning, migration, genera](/img/f6/6685902fca43163cb63cf2c55d76c6.jpg)
[cvpr2022] CMU tutorial on multimodal machine learning, 200+ pages to explain the knowledge of multimodal learning system with six challenges of representation, alignment, reasoning, migration, genera

Big Fish eating Little Fish Games version complète

zabbix6.0+timescaledb+企业微信告警

SQL语句知识点有哪些

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(一)——整体系统设计

Implementation of assembly language greedy snake and Tetris dual task design (II) -- detailed design of greedy snake

【CVPR2022】CMU《多模态机器学习》教程,200+页阐述表示、对齐、推理、迁移、生成与量化六大挑战的多模态学习系统知识
随机推荐
Anfulai embedded weekly report (issue 270): June 13, 2022 to June 19, 2022
mysql如何对列求和
Resttemplate multiple authentication information authorization
粗读Targeted Supervised Contrastive Learning for Long-Tailed Recognition
MySQL 5.7编译安装
I remember that procedure cannot be written in maxcomputer. If you want to use Oracle procedure
Software testing office tools recommendation - Desktop Calendar
How to query the data in MySQL
JVM内存结构
Goldfish rhca memoirs: do447ansible tower navigation
最高月薪17K,只要心中有一片希望的田野,勤奋耕耘将迎来一片翠绿~
NetCore3.1 ping网络是否畅通及获取服务器Cpu、内存使用率
如何查询mysql中所有表
通过flinksql 的方式使其部分字段更新可以么?
What are the knowledge points of SQL statements
RecycleView懒加载失效问题(二)
大鱼吃小鱼小游戏完整版
【CVPR2022】CMU《多模态机器学习》教程,200+页阐述表示、对齐、推理、迁移、生成与量化六大挑战的多模态学习系统知识
Now CDC supports MySQL 5 What time is it? Previously, it seemed that it was 5.7. Today, it is found that the MySQL data source of 5.6 can also be updated in real time
范畴(Category)