[This article is from the Sky Cloud-owned blog Park]
1. Recursively traverse all files in the directory and locate the specified format string through the Finder function
2. The Finder function used to find a string is defined by itself, and here a ip_port_finder is defined by a regular expression to find the Ip:port format (coarse match: numbers. Numbers. Numbers. Numbers: Numbers) string
3, using gevent to achieve the time-consuming task of co-process concurrent completion
The code is as follows:
#-*-coding:utf-8-*-ImportRe fromOs.pathImportJoin fromOsImportWalk fromGeventImportMonkeyImportGeventmonkey.patch_all ()defIp_port_finder (STR:STR)Bool:pattern= Re.compile (r". +\d+\.\d+\.\d+\.\d+:\d+") Matchobj=Pattern.match (str)ifMatchobj:Print("------") Print(f"Target found: {matchobj.group (0)}") returnTrueElse: returnFalsedeffind_in_file (File_path, Finder): With open (File_path,"R", encoding="Utf-8", errors='Ignore') as F: for(num, value)inchenumerate (f):ifFinder (value):Print(f"file path: {File_path}") Print(f"number of rows: {num}") find_in_path_recursively=LambdaPath, Finder:gevent.joinall ([Gevent.spawn (Find_in_file, join (Root, file_name), finder) forRoot, directories, f_namesinchWalk (Path) forfile_nameinchF_names])if __name__=='__main__': Path="E:\dev_codes\xxx"find_in_path_recursively (Path, Ip_port_finder)
Reference article:
Python Multi-task implementation concurrent download multi-image
Python3 write a file string finder with Gevent