当前位置:网站首页>How to find the desired file among thousands of files on your computer?
How to find the desired file among thousands of files on your computer?
2022-06-21 20:33:00 【Minimalist lesson】
This article mainly introduces the use of Python Write segment applet , To quickly find the files you want .
I don't say much nonsense , On the first code , And then explain .
import os
def findfiles(name, path):
# Define an array to hold the search results
search_result = []
# utilize os.walk Function to traverse all files in the path
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 The function will take list Returns the subscript and value of the list in the form of
for index, value in enumerate(search_result):
# In the search results , By serial number 、 File name branch printout
print("[" + str(index+1) + ",", "'" + value + "'" + "]")
if __name__ == '__main__':
search_path = input(" Please enter the path to search :")
file_name = input(" Please enter the name of the file you want to find :")
findfiles(file_name, search_path)This code is very simple , There are two main functions involved , One is os Modular walk function , One is the built-in function enumerate.
1. So let's see walk function ,os.walk Function is to traverse all the files , What does that mean ? If you still have folders under the current search path , Will continue to go to this folder to find files , Go down one layer at a time , Until the end there is no folder .
walk The function returns three values :
dir_path: Current search path
dir_names: All existing folders under the current path
filenames: All existing files in the current path
For example, the following example , In the folder "100days" below , There is one "day01" Folder , But there are no documents , So the third value is an empty list , And then I'll go into "day01" Folder continue to find files , Found that there is no folder , Only two files .
F:\Study\Python\100days ['day01'] []
F:\Study\Python\100days\day01 [] ['flag.py', 'peppa_pig.py']2. enumerate The function is python Built in functions , For traversing a data object ( As listing 、 Tuples or strings ) Combined into an index sequence , List both data and data index , Generally used in for Cycle of .
What does that mean ? Is in the for For recycling enumerate Act on a traversable object , Not only will it return every value in the object , It also returns the subscript corresponding to the value , Sometimes we need to use the subscript corresponding to the value , So I'll use enumerate function , Write a small code to demonstrate
names = [" Zhang San ", " Li Si ", " Xiao Ming ", " name "]
for index, name in enumerate(names):
print(index, name) Output results :
0 Zhang San
1 Li Si
2 Xiao Ming
3 name So we know that Zhang San is in the first position , Li Si is in the second position and so on .
Finally, let's take a look at the overall program execution results :
For example, on the computer F:\Study\Python Find the name under the path that contains day The file of , give the result as follows :
Expand :
os There is also a function under the module to find files , Namely os.listdir, and os.walk The difference is that ,os.listdir Only all folders and files under the current path will be returned , Will not continue to drill down to the next folder to continue to find .
边栏推荐
- Whether there are keywords in MySQL
- mysql与oracle有没有区别
- Uniapp applet opens the map and selects the location demo effect wx Chooselocation
- 我记得MAXCOMPUTE里面是不可以写PROCEDURE,那如果想oracle procedure
- IAR major upgrade, support vs code, St release the first sensor with processing unit
- 简单使用JS
- Delete the penultimate node - linked list topic
- inno setup 安装路径框学习
- Recycleview lazy load failure
- 【基于合泰HT32F52352的智慧垃圾桶总结】
猜你喜欢

【时序预测完整教程】以气温预测为例说明论文组成及PyTorch代码管道构建
![[dry goods knowledge] redis: from the application to the bottom, one article will help you](/img/d3/084f6e69f7196d9e10c98bdfe71e1c.png)
[dry goods knowledge] redis: from the application to the bottom, one article will help you

Harbor高可用集群设计及部署(实操+视频),基于离线安装方式

Big Fish eating Little Fish Games version complète

How MySQL sums columns

【ICML2022】CtrlFormer: 通过Transformer学习视觉控制的可迁移状态表示

软件测试办公工具推荐-桌面日历

研报精选 | 麦肯锡《中国数字创新未来》趋势解读

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

BTC投资者损失预计达73亿美元!“割肉式”抛售来袭?加密寒冬比预期更冷、更长!
随机推荐
金鱼哥RHCA回忆录:DO447管理用户和团队的访问
Daily development of common tools to improve efficiency
SD training 6.21 summary
Big Fish eating Little Fish Games version complète
Model evaluation and selection of machine learning
汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(二)——贪吃蛇详细设计
mysql如何查询第几条数据
Inno setup installation path box learning
mysql增加的语句是什么
Zabbix6.0+timescaledb+ enterprise wechat alarm
Inno setup change installation path learning
Uniapp applet opens the map and selects the location demo effect wx Chooselocation
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline
Is it safe to open a margin account? What are the requirements?
[wechat applet failed to change appid] wechat applet failed to modify appid all the time and reported an error. Tourist appid solution
In May, I just came back from the Ali software testing post. I worked for Alibaba P7 at 3+1, with an annual salary of 28*15
Points cloud to Depth maps: conversion, Save, Visualization
【基于合泰HT32F52352的智慧垃圾桶总结】
The highest monthly salary is 17k. As long as there is a field of hope in your heart, hard work will usher in a green land~
Learn from the old guard on the development of harmonyos: in the name of the father, code force is fully open! Write a section of harmonyos wishing father's Day