Analog Grep-rl "python" F:\xuyaping this command
#查看xuyaping文件夹所有的绝对路径import Osg=os.walk ("f:\\xuyaping") #g为迭代器for i in G: # Print (i) #i为文件路径 for J in I[-1]: file_path= "%s\\%s"% (i[0],j) print (File_path)
Program Output Result:
F:\xuyaping\xuyaping.txt.txtf:\xuyaping\xuyaping1.txt.txtf:\xuyaping\a\a.txt.txtf:\xuyaping\a\a1\a1.txt.txtf:\ Xuyaping\a\a1\a2\a2.txt.txtf:\xuyaping\b\b.txt.txt
The code is as follows:
#模拟grep-rl "python" F:\xuyaping this command import os,time# initialize the decorated function so that it runs to the line where yield is located def init (func): Def wrapper (*args,**kwargs ): Res=func (*args,**kwargs) Next (RES) return res return Wrapper#search function uses loops to find an absolute path to the next stage opener function Pass it once, until the pass is finished. # so the opener function loops the collection, so the opener function uses a while loop. @initdef Search: #定义生成器target for delivery path File_path "Absolute path to found file" while True:dir_name=yield print (" Shop Search Start Production: absolute path to File ") Time.sleep (1) g = Os.walk (dir_name) for I in G:for J in I[-1]: File_path= "%s\\%s"% (i[0],j) target.send (file_path) #传递路径file_path, give opener function @initdef opener (target): "Open file, get file handle" while True:file_path=yield #opener函数接收search函数传递过来的路径file_path. The opener function passes the execution result to target print in the search function ("Shop opener start production product: File Handle") Time.sleep (1) with open (File_path) as F : Target.send ((file_path,f)) #同样传递句柄f and File_path to the cat function, so this time with the generator target to pass, as above @initdef Cat (target): "Read the file contents"While True:file_path,f=yield #cat函数接收opener函数传递过来的句柄f and File_path. The cat function passes the execution result to target print in the opener function ("Shop cat starts production: A line of the file") Time.sleep (1) for lines in F:ta Rget.send ((file_path,line)) @initdef grep (pattern,target): "Filter for Python in one row" while True:file_path,line=yield Print ("Shop grep begins production: File path containing Python's line of content") Time.sleep (1) if pattern in Line:target.send (fil E_path) @initdef printer (): "Print file path" while True:file_path=yield print ("Workshop printer start production: Get final Product") Time.sleep (1) print (File_path) #g =search ("target")--->g=search (opener (target))--->g=search (Opener (Cat ( target)) #--->g=search (opener (Cat (grep)))--->g=search (opener (Cat (grep ("Python"), printer ( )))) G=search (Opener (Cat (grep ("Python", Printer ()))) G.send ("f:\\xuyaping")
Run Result:
Workshop Search starts production: The absolute path of the file workshop opener started production of products: File handle shop Cat starts production product: A line of the file shop grep begins to produce products: file paths that contain the Python line shop grep begins production: A file path that contains the contents of the Python line Path shop grep begins production: File paths that contain Python this line of content shop grep begins production: A file path containing the Python line workshop printer started production: Get the final product F:\xuyaping\ Xuyaping.txt.txt Workshop grep begins production: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep begins production: A file path containing the Python line shop opener start production Product: File Handle shop cat starts production: A line of content in the file shop grep begins production: A file path that contains the Python line shop grep starts production: file path with Python this line of content shop grep starts production: file path workshop with Python line grep starts production: file path containing python this line of content workshop printer started production: Get the final product F:\xuyaping\ Xuyaping1.txt.txt Workshop grep begins production: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep begins production: A file path that contains the contents of the Python line workshop opener started Product: File Handle shop cat starts production: A line of content in the file shop grep begins production: A file path that contains the Python line shop grep starts production: file path containing python this line of content shop grep starts production: A file path that contains the Python line. Car grep begins production: A file path that contains a python line of content shop grep begins production: A file path that contains the Python line shop grep starts production: file path with Python this line shop opener start production: file Handle shop cat starts production Product: A line of content in a file shop grep starts production: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep starts productionProduct: File path containing python this line of content shop grep begins production: A file path containing the Python line workshop printer started production: Get the final product F:\xuyaping\a\a1\ A1.txt.txt Workshop opener started production: file Handle shop Cat starts production product: A line of files shop grep begins production: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep begins production Product: A file path containing the Python line the shop grep begins to produce a product: A file path that contains a python line of content shop grep begins to produce a product: A file path that contains the Python line shop grep begins to produce a product: A file path that contains the contents of Python shop opener Start production: File Handle shop cat starts production: A line of content in the file shop grep begins production: A file path that contains the Python line workshop printer began to produce products: Get the final product F:\xuyaping\b\ B.txt.txt Workshop grep begins production of a product: A file path that contains the Python line shop grep begins production: A file path that contains the Python line shop grep starts production: file path with Python this line of content shop grep begins production: Includes Pyth On the file path of this line of content
Application of the Python association function