title: How do I find the first instance of a string in a text file using Python?
body: <p>I am trying to search text files for the first instance of string and return everything else in that first line, but my current program finds and returns the last line with the string.</p>
  
  <p>Any idea what I need to do?</p>
  
  <p>Thanks!</p>
  
  <p>This is what my code looks like:</p>
  
  <pre><code>#Open search file as read only
  with open(fullpath, 'r') as searchfile:
  
       #Clear variable names
       partname = partsize = None
  
       #Search file for strings, trim lines and save as variables
       for line in searchfile:
  
           if "PART FILE NAME" in line:
               x = line
               partname = x[18:-1]
  
           if "PART SIZE" in line:
               y = line
               partsize = y[18:-1]
       #Open csv file, write variables, close csv file
       storefile = open("C:/Documents and Settings/Desktop/blue pega3.csv", 'a')
       storefile.write("%s,%s,%s\n" %(partname, partsize, fullpath))
       storefile.close()
       #Close search file
       searchfile.close()         `
  </code></pre>