Python - failing to read correctly the first line of a text file to a list
<p>I'm having a problem understanding why my python program does what it does when reading (first) lines from files and adding the lines into a list. For some reason the first line needs to be empty or it'll not read the first line correctly. If the first line is empty, it's not empty (at least not according to python).
The thing is, I have two types of files:</p>

<p>First file is in the form:</p>

<pre><code>text:more text
another text:and more
</code></pre>

<p>and the second file in the form:</p>

<pre><code>text_file.txt
anothertext_file.txt
</code></pre>

<p>Both files are UTF-8 encoded text files. The first line of both files that gets added to a list in my program, is "text" and "text_file.txt" but any code that for example tries to say </p>

<pre><code>if something == "text":
    ...
</code></pre>

<p>will not get executed even if the "something" <strong>is</strong> the same as the "text".</p>

<p>So I'm assuming that my problem is that somewhere in the machine code (or something), my computer writes some invisible code in the beginning of the text file and that makes the first line not what it is. Maybe? I have actually found a solution for the problem simply by adding an empty line and an if clause when reading the file line by line:</p>

<pre><code>if not "." in line:
    ...
</code></pre>

<p>and in the other filetype:</p>

<pre><code>if not ":" in line:
    ...
</code></pre>

<p>Those if clauses work and my program does what it's supposed to (as long as I always add an empty line to the beginning of the file), but I haven't been able to find a real reason for why my program is behaving as it is. Also, I would like to not have to do this kind of a workaround if there's an easier solution that doesn't involve me editing all my files and adding an if clauses to my code.</p>

<p>Would appreciate any help understanding what's happening here!</p>

<p>Edit: as you people have been asking for my code, here it is:</p>

<pre><code>filelist = []
with open("filename.txt", "r", encoding="UTF-8") as f:
for line in f:
    filelist.append(line.rstrip("\n"))
</code></pre>

<p>This does not work properly. Also I tried it like mxds said, </p>

<pre><code>filelist = []
with open("filename.txt", "r", encoding="UTF-8") as f:
lines = f.readlines()
for line in lines:
    filelist.append(line.rstrip("\n"))
</code></pre>

<p>and this does not work either. It is only a problem in the files in the first character of the first line.</p>

<p>Edit2:
It seems the problem is having a Byte order mark in the beginning of my text files. After a quick googling I didn't find a solution as to how I could remove it. I'm creating my files with just windows notepad.</p>

<p>Final edit: 
Apparently notepad is not a real text editor. I guess I'll just swap over from notepad to notepad++ to avoid this problem. However, just in case I'll have to handle my files in notepad: If I open a textfile in notepad and add some text in it, will it add a BOM or should it do that only in the creating of the file?</p>