Jobs - Find the file contents



# 1, access to all the files in a directory below
# 2, to determine whether a .txt ending
# 3, if this is the end of the open .txt file, read the contents of
# 4, to determine whether the keyword in the content of the document inside
# 5 If the output, then on
Import os


DEF find_content (keyword, path):
IF os.path.exists (path):
for cur_path, dirs, Files in os.walk (path):
for Files in File:
IF file.endswith ( '.txt'):
abs_path = os.path.join (cur_path, file)
with Open (abs_path) AS f:
IF keyword in f.read ():
Print ( '[% s] in this document which you have to find content '% abs_path)!

the else:
Print (' you pass path does not exist ')


Guess you like

Origin www.cnblogs.com/kexinwang/p/11105535.html