Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
One, foreword
There are many situations where we need to do a file search. Such as looking for a long time ago file, or want to delete duplicate files and so on. The former just needs the name to look up the file, while the latter also needs to look up the contents of the file. Today we will realize the operation of file search.
2. Fnmatch module
1, traverse
Before searching for files, we have a key action, which is to traverse the folder. This part of the code is as follows:
import os
basedir = r"D:\\"
for root, dirs, files in os.walk(basedir):
for file in files:
path = os.path.join(root, file)
print(path)
Copy the code
Where path is the absolute path of the file we traversed. With this we can start searching the files.
2, search,
There is a built-in fnmatch module in Python that we can use to match directories as follows:
from fnmatch import fnmatch
# match directory
match = fnmatch("test.jpg"."test.jpg")
print(match)
Copy the code
We call the fnmatch function, passing in what to match and the matching rules. Here we directly match two strings and delete the result as follows:
True
Copy the code
It will return a bool. But this is the same as if, the fnmatch module has some special functions. The answer is yes. In addition to direct matching, fnMatch also supports wildcard operations, such as the following code:
from fnmatch import fnmatch
match = fnmatch("test.jpg"."*.jpg")
print(match)
Copy the code
Here we use test.jpg and *.jpg to match. Where * represents any number of characters, i.e., files that match the end of.jpg. There are other wildcards besides *, as follows:
The wildcard | role |
---|---|
* | Match all characters |
? | Matching a single character |
[seq] | Matches characters in the specified range |
[!seq] | Matches characters outside the specified range |
We can write matching rules as required. In addition to the fnmatch function, there is also a filter function in fnmatch, which I won’t expand here.
File search
We combine the operation of traversal and match to write the function of file search, the code is as follows:
import os
from fnmatch import fnmatch
basedir = r"D:\\"
for root, dirs, files in os.walk(basedir):
for file in files:
path = os.path.join(root, file)
# Match all giFs on drive D
if fnmatch(path, "*.gif") :print(path)
Copy the code
Above is the search D disk under all GIF operation, we can modify according to their own needs. If you want to find a file with test in the path, you can change it to the following:
import os
from fnmatch import fnmatch
basedir = R D: \ ","
for root, dirs, files in os.walk(basedir):
for file in files:
path = os.path.join(root, file)
if fnmatch(path, "*test*") :print(path)
Copy the code
So here we’re changing the matching rules.