Add a line to a text file if it not exist using python
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/12275255/add-a-line-to-a-file-if-it-not-exist-using-python">Add a line to a file if it not exist using python</a>  </p>
</blockquote>



<p>I have a text  file as follows:</p>

<pre><code>  &lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0"&gt;
  &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
  &lt;ImportGroup Label="ExtensionTargets"&gt;
  &lt;/ImportGroup&gt;
&lt;/Project&gt;
</code></pre>

<p>I want to add a line <code>&lt;Import Project="$(ProjectName).targets" /&gt;</code> between
<code>&lt;/ImportGroup&gt;</code> and <code>&lt;/Project&gt;</code> as follows </p>

<pre><code>  &lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0"&gt;
  &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
  &lt;ImportGroup Label="ExtensionTargets"&gt;
  &lt;/ImportGroup&gt;
  &lt;Import Project="$(ProjectName).targets" /&gt;
&lt;/Project&gt;
</code></pre>

<p>I used fileinput module to insert the line but i don't want to add 
<code>&lt;Import Project="$(ProjectName).targets" /&gt;</code> if it  already exists in file 
How can I do that?</p>
