IronPython add Unicode characters at the start of a file
<p>I use a script to update the version of each AssemblyVersion.cs file of a .NET project. It always worked perfectly, but since a format my PC, it adds unicode character at the start of each .cs file edited. Like this:</p>

<pre><code>Ã¯Â»Â¿ÃÂ¯ÃÂ»ÃÂ¿using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
</code></pre>

<p>I use this code to open a file:</p>

<pre><code>with open(fname,  "r") as f:
    out_fname = fname + ".tmp"
    out = codecs.open(out_fname, "w", encoding='utf-8')
    textInFile=""
    for line in f:
        textInFile += (re.sub(pat, s_after,line))
    out.write(u'\uFEFF')
    out.write(textInFile)
    out.close()
os.remove(fname)
os.rename(out_fname, fname)
</code></pre>

<p>I've also tried, as wrote <a href="http://stackoverflow.com/questions/19591458/python-reading-from-a-file-and-saving-to-utf-8">here</a>, to use <code>io</code> instead of <code>codecs</code>, but nothing is changed.</p>

<p>On other teammates' PCs it works with the same configuration (Win10 and IronPython 2.7).</p>

<p>What can I try to solve this issue? Where can I looking for the problem?</p>

<p>Thanks</p>

<p>It seems that the files at your file system are using ISO-8859-1 encoding, while you are adding the UT8 BOM marker at the beginning of each file. </p>

<p>After your code does it's job, you get a file with UTF-8 BOM + ISO-8859-1 meta at the beginning.</p>

<p>I would check the encoding of your input files before modification with Notepad++ (or any other editor) just to see if the scenario I described is valid. If it is, you will need to read your input files with a different encoding in order to avoid the metadata:</p>

<pre><code>with open(fname,  "r",  "ISO-8859-1") as f:
    ...
</code></pre>

c# python unicode encoding ironpython