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

What is the method for python to find files?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what is the method of finding files in python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of finding files in python"?

The fnmatch library of the standard library is specifically used for file name matching and supports string matching using wildcards.

1. Fnmatch: determine whether the file name conforms to a specific pattern

2. Fnmatchcase: determines whether the file name conforms to a specific pattern, and is not case-sensitive

3. Filter: returns the list of file names that match a specific pattern in the input list

4. Translate: converts wildcard patterns to regular expressions.

The fnmatchcase function is almost the same as the fnmatch function, except that the case of letters in the file name is ignored when matching the file name.

The filter function is similar to the fnmatch function, except that fnmatch matches one file name at a time, and the filter function matches a set of file names at a time. The filter function takes the list of file names as the first parameter and the file name pattern as the second parameter, and then returns all the file names that match the pattern in the input list as a list.

Import os,fnmatchnames = os.listdir ('.') for name in names: if fnmatch.fnmatch (name,'*.xlsx'): print (name) if fnmatch.fnmatch (name,' [a murz] *'): print (name) print (fnmatch.filter (names, "[a murz] * .xlsx"))

Currently, to get a specific type of file list, we first get the file list through os.listdir, and then filter it by using fnmatch for file name pattern matching. In Python, there is an even easier way to use the glob library of the standard library. The role of glob is equivalent to os.listdir plus fnmatch. After using glob, you don't need to call os.listdir to get the file list, just use pattern matching.

Import globprint (glob.glob ('* .xlsx') print (glob.glob ('[amerz] *'))

The previous examples are to find files in a directory and use pattern matching to select the file type you want. In the course of actual work, you are more likely to find all the files in a directory and its subdirectories. For example, find pictures in a directory and its subdirectories. You can use the walk function of the os module. The walk function traverses a directory and its subdirectories, and for each directory, walk returns a triple (dirpath, dirnames,filenames). Where dirpath saves the current directory, dirnames is the list of subdirectories in the current directory, and filenames is the list of files in the current directory.

Import os,fnmatchimages = ['* .jpg','* .jpeg','* .png','* .tif','* .tiff'] matches = [] for dirpath,dirnames,filenames in os.walk (os.path.expanduser (r "D:/test")): for image in images: for filename in fnmatch.filter (filenames,image): matches.append (os.path.join (dirpath,filename)) print (matches)

When traversing a directory and its subdirectories, if you want to ignore a subdirectory, you can directly modify the dirnames in the triple, that is, remove the directory that needs to be ignored from the dirnames list.

If 'extest' in dirnames: dirnames.remove (' extest') at this point, I believe you have a deeper understanding of "what is the method of finding files in python". 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

Development

Wechat

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

12
Report