trying to find a substring in a string

Terry Reedy tjreedy at udel.edu
Sat Jun 21 19:11:11 EDT 2008



barnacle.steve at gmail.com wrote:
> I'm trying to write what should be a simple script in Python, which
> I've never ever used before.
> 
> Essentially, I have a text file that has a list of full path file
> names to other files, separated by carriage returns.
> Contents of first file:
> c:\blah.txt
> c:\blah1.txt
> c:\blah2.txt
> 
> The goal is for the user to specify another file, and then search the
> specified file for instances of files from the first file.
> Contents of user specified file:
> file = "c:\blah.txt"
> file = "c:\blah1.txt"
> 
> My goal is for the program to tell me that it found c:\blah.txt and c:
> \blah1.txt.
> 
> I've read the contents of the existing file into an array, where each
> element is a line from the file. 

Put each stripped (to delete \n) line into a set.  Then parse out the 
filenames and check that they are in the set.  Something like

def getname(line): <whatever)

s=set(line.strip() for line in open('allfiles.txt', 'r'))
for line in open('paths.txt'):
   if getname(line) not in s:
     return '%s not found'%line
else:
   return 'all found'

tjr




More information about the Python-list mailing list