Why is there a trailing none when reading text in python?
<p>I'm trying to write a text reader program that can differ a text into three parts: title, tag, and content.</p>

<p>what happen is that it's giving me a 'None' value at each end of the content.</p>

<p>here is the content reader code:</p>

<pre><code>#counting lines in the text
def bufcount(file):
    file.seek(0)
    lines = 0
    buf_size = 1024 * 1024
    read_f = file.read # loop optimization

    buf = read_f(buf_size)
    while buf:
        lines += buf.count('\n')
        buf = read_f(buf_size)
    return lines

#for reading the content
def searchForTheContent(file):
    count=bufcount(file)
    file.seek(0)
    i=3 #to read after the third line, which is the content
    lines=file.readlines()
    while i&lt;count:
        i=i+1
        #print(i)
        if lines[i]=="\n":
            pass
        if lines[i]!="\n":
            print(lines[i])
</code></pre>

<p>Calling the Function:</p>

<pre><code>path= '.\\Texts\\*.txt'
files = glob.glob(path)

for name in files:
    file= open(name)
    print(searchForTheContent(file))
</code></pre>

<p>Result:</p>

<pre><code>safahsdfhajfha
dfasdfsdfsadf

sadfasdfasdfasdfasdf

asdfasfdasd
None
</code></pre>

<p>Where is that 'None' value come from? and any suggestion how to remove it?</p>

<p>You are printing the return value for the function:</p>

<pre><code>print(searchForTheContent(file))
</code></pre>

<p><code>searchForTheContent()</code> has no explicit <code>return</code> statement, so <code>None</code> is returned, and you are printing that. You'd get the same result with an empty function:</p>

<pre><code>&gt;&gt;&gt; def foo(): pass
...
&gt;&gt;&gt; print(foo())
None
</code></pre>

<p>Remove that <code>print()</code> call:</p>

<pre><code>for name in files:
    file= open(name)
    searchForTheContent(file)
</code></pre>

<p>Just to be explicit: <code>print()</code> doesn't 'return' anything to the caller; <code>print()</code> sends output to the <code>stdout</code> stream, which usually is connected to your terminal which is why you see the output there. That output is not also given as a return value.</p>

python