Modify/replace a line in a python file using another python file
<p>I am trying to replace/modify a part of string in a python file from another python file. </p>

<p>The line I am trying to replace in other PY is :</p>

<pre><code>a.setSystemFile('D:/test/f.xml')
</code></pre>

<p>I would like to replace the part of this line i.e. the xml path string with different xml path:</p>

<p>Example: </p>

<pre><code>a.setSystemFile('C:/try/X.xml')
</code></pre>

<p>My code looks like:</p>

<pre><code>with open('script.py') as f:  lines = f.read().splitlines()
with open('script.py', 'w') as f:

    for line in lines:
      if line.startswith('a.setSystemFile'):

        f.write(line.replace('D:/test/f.xml','C:/try/X.xml')
</code></pre>

<p>This however, renders the file empty and only writes C:/try/X.xml. Is there a way to preserve the original content at the same time replace just the XML path string like in above example.</p>

<p>Any help would be appreciated. Thanks.</p>
