Home > other >  Extract the directory c file contains the keyword line
Extract the directory c file contains the keyword line

Time:09-21

Directory c file contains the keyword line, auxiliary in c programming, sharing,

The import of OS, re
The class SearchFile (object) :
Def __init__ (self, path=') :
Self. _path=path
Self. Abspath=OS. Path. Abspath (self. _path) # default current directory

Def findLines (self, keyword, root) :
Filelist=[]
For root, dirs, files in OS. Walk (root) :
For the name in files:
FileName=re. Match (". * \. C $", OS. The path. The join (root, name))
If fileName:
Fitfile=filelist. Append (OS) path) join (root, name))
Print ('... ')
For filepath filelist in:
If OS. Path. Isfile (filepath) :
# to open the file to find if there is a corresponding keyword
Fp=open (filepath, 'r')
Try:
Lines=fp. Readlines ()
Except:
Print (" open file "+ filepath +" abnormal ")
The else:
K=0
For strtemp in lines:
Out=re. Match (" * "+ keyword +". * ", strtemp)
If the out:
Print (strtemp)

Def __call__ (self) :
While True:
Workpath=input (' search the current directory? Y/N: ')
If (workpath==") :
Break
If workpath=='y' or workpath=='y' :
Root=self. Abspath # the current working directory as a working directory
Print (' directory: 'root)
# # dirlist=OS. Listdir () lists work files and directories in the directory
# print (dirlist)
The else:
Root=input (' input search directory:)
Print (' directory: 'root)
Keyword=input (' need to enter empty the default search keywords, keyword \ "static const char * " : ')
If (keyword==") :
Keyword="static const char * '
Self. FindLines (keyword, root) # lookup with characters of the specified file

If __name__=="__main__ ':
Search=SearchFile ()
Search ()
  • Related