I'm coding for this simple requirements: Search for a key word and return the file name that has such key word
This is the first part of the code to search for 'txt' files. But I'm having problem with looping over file names: code just shows 1 result (file) while it is expected to list all file names.
import os
#list file names
def list_file_name(path):
fileList = os.listdir(path)
return(fileList)
#Function 1: search key_word in txt file
def search_txt(path, keyWord):
for file in list_file_name(path):
if file.endswith('txt'):
f = open(path + '/' + file, 'r')
openFile = f.read()
if keyWord in openFile:
return('Key word {} is in {}'.format(keyWord, file))
else:
return('No key word found')
continue
#run the function
print(search_txt(input('Please input folder path: '), input('Please input key word: ')))