Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a simple file lookup program with python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use python to write a simple file search program", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to write a simple file search program with python".

Before we start writing code, we have to analyze the problem. I have the following requirements for file search:

1. I can specify the directory of files to search

2. I can specify the keyword, and the program looks up the name of the file that contains the keyword in the specified directory.

3. I can specify the file suffix, and the program looks for the file name that matches this suffix in the specified directory.

4. If both keywords and suffixes are specified, the intersection of their search results is taken.

All right, after we figure out the target, we can do it.

First of all, we have to solve the problem of file lookup. How can we get all the file names in a directory? Fortunately, the os module provides a method called walk, which can do this. Specific usage, we can Baidu, key words: python os.walk.

When both keywords and suffix names are specified, we need to take their intersection. We can use set to store the results that match the keyword and suffix name respectively, and then find the intersection.

It is possible to specify only keywords but not suffixes, but none of them is allowed, which requires the program to be flexible in dealing with such parameter changes, so we use variable parameters to solve the problem.

After the previous analysis, the code is simple.

# coding=utf-8

Import os

Def find_file (path,**kwargs):

If not 'key' in kwargs and not' suffix' in kwargs:

Print u 'Please specify a keyword or suffix'

Return

Key = kwargs.get ('key',None)

Suffix = kwargs.get ('suffix',None)

File_lst = []

For parent,dirnames,filenames in os.walk (path):

For filename in filenames:

Filepath = os.path.join (parent,filename)

File_lst.append (filepath)

Suffix_set = set ()

Key_set = set ()

For filepath in file_lst:

Name = os.path.split (filepath) [1]

If not suffix = = None and name.endswith (suffix):

Suffix_set.add (filepath)

If not key = = None and not-1 = = name.find (key):

Key_set.add (filepath)

If suffix and key:

Res_set = suffix_set.intersection (key_set)

Elif suffix:

Res_set = suffix_set

Else:

Res_set = key_set

Return list (res_set)

If _ _ name__ = ='_ _ main__':

File_list = find_file ('/ Users/kwsy/PycharmProjects/Dream',key='lead',suffix='.py')

For name in file_list:

Print name

At this point, I believe you have a deeper understanding of "how to use python to write a simple file search program". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report