On 28/12/2014 17:27, Seymore4Head wrote:
I need to search through a directory of text files for a string.
Here is a short program I made in the past to search through a single
text file for a line of text.

How can I modify the code to search through a directory of files that
have different filenames, but the same extension?

fname = raw_input("Enter file name: ")  #"*.txt"
fh = open(fname)
lst = list()
biglst=[]
for line in fh:
     line=line.rstrip()
     line=line.split()
     biglst+=line
final=[]
for out in biglst:
     if out not in final:
         final.append(out)
final.sort()
print (final)


See the glob function in the glob module here https://docs.python.org/3/library/glob.html#module-glob

Similar functionality is available in the pathlib module https://docs.python.org/3/library/pathlib.html#module-pathlib but this is only available with Python 3.4

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to