title: how to read and overwrite part of batch file in Python
body: <p>I have a batch file look like something below:</p>
  
  <pre><code>.....
  set ARGS=%ARGS% /startDate:2015-07-15T15:20:00.000
  set ARGS=%ARGS% /endDate:2015-07-15T17:30:00.000
  set ARGS=%ARGS% /IDs:250
  set ARGS=%ARGS% /values:10000,20000
  .....
  </code></pre>
  
  <p>now I want to read it and overwrite it with new dates (1 day after current start and enddate). My code below works fine if I write it to a new file but doesn't work if I tried to overwrite it. Any idea about how to fix it?</p>
  
  <pre><code>WANTED = 19 #or however many characters you want after dates
  
  with open('myfile.bat') as searchfile, open('mynewfile.bat', 'w') as outfile:
  
  
      for line in searchfile:
          left,sep,right = line.partition('startDate:')
          if sep: # True iff 'Figure' in line
              startdatestr = (right[:WANTED])
              startdate = datetime.strptime(startdatestr, "%Y-%m-%dT%H:%M:%S")
              newstartdate = startdate + timedelta(days=1)
              newstartdatestr = newstartdate.strftime("%Y-%m-%dT%H:%M:%S")
              line = line.replace(startdatestr, newstartdatestr)  
  
  
          left,sep,right = line.partition('endDate:')
          if sep: # True iff 'Figure' in line
              enddatestr = (right[:WANTED])
              enddate = datetime.strptime(enddatestr, "%Y-%m-%dT%H:%M:%S")
              newenddate = enddate + timedelta(days=1)
              newenddatestr = newenddate.strftime("%Y-%m-%dT%H:%M:%S")
              line = line.replace(enddatestr, newenddatestr)  
          outfile.write(line)  
  </code></pre>