title: Replace string with wildcard in a .txt file with Python
body: <p>I'm still new to scripting and am not sure the best method to accomplish what I am trying to do. This will be the first python script I am trying to write. Please note, I am using <strong>Python2.7</strong> for this.</p>
  
  <p>I want to write a batch file for a user with an argument given. This argument will be a path. This path will change everyday and be used to execute job tests. I want to invoke it with <code>Nightly.bat "build path"</code></p>
  
  <p>Here's what I want to accomplish:</p>
  
  <p><strong>1. Batch file ensures the path exists with health check.<br>
  2. Batch file executes the python file with the given variable.<br>
  3. Python file finds a string in the testrun script with the build path, and will replace that path with the given variable.<br>
  4.Batch file executes the testrun selenium script.</strong></p>
  
  <p>Here is the code Python Code:</p>
  
  <p>test1.txt contents:</p>
  
  <pre><code>blah
  This is a first string
  </code></pre>
  
  <p>nightly.py contents:</p>
  
  <pre><code>import sys
  import shutil
  import os
  import re
  
  tf = open('tmp', 'a+')
  string = "This is "
  
  with open('test1.txt') as f:
      for line in f.readlines():
              string = re.sub ('This is .*', 'This is a second string', string)
  
  shutil.copy('test1.txt', 'tmp')
  tf.write(string)
  f.close()
  tf.close()
  </code></pre>
  
  <p>after executing the nightly.py file, this is the tmp file contents:</p>
  
  <pre><code>blah
  This is a first stringThis is a second String
  </code></pre>
  
  <p>I need to make it so <code>This is a first string</code> is replaced by <code>This is a second string</code></p>
  
  <p>In the end, the tmp file should have the following contents:</p>
  
  <pre><code>blah
  This is a second string
  </code></pre>
  
  <p>Thanks for continuing to try.</p>
  
  <pre><code>*****************************
  * Updated for Kirbyfan64sos *
  *****************************
  </code></pre>
  
  <p>nightly.py contents:</p>
  
  <pre><code>import sys
  import shutil
  import os
  
  tf = open('tmp', 'a+')
  with open('test1.txt') as f:
      for line in f.readlines():
          if line == 'This is*':
              line = 'This is a second string' 
          tf.write(line)
  f.close()
  tf.close()
  shutil.copy('tmp', 'test1.txt')
  os.remove('tmp')
  </code></pre>