-2

I'm trying to read a text file with a blank line at the end of it, and I need to put those lines into a list.

The text file looks something like this:

this is the first line
this is the second line
*
(* is indicating a blank line)

i read the textfile like this:

read_str = file.readlines()
print(read_str)

It gives me the list I want, which is good, but at the end it says "None". Like this:

['this is the first line\n', 'this is the second line\n']
None

Why is it giving me "None" at the end, and how can I fix this?

Sil
  • 1
  • 1
  • 3
  • 1
    Can you paste all the code here? – Zaur Nasibov Oct 22 '13 at 13:21
  • 4
    I have a feeling that the code you posted is inside a function that doesn't `return` anything. And then you do `print my_function_that_doesnt_return_anything()`. That's usually the cause of this sort of problem. – Kevin Oct 22 '13 at 13:23
  • Related: [Random 'None' output from basic python function](http://stackoverflow.com/questions/7053652/random-none-output-from-basic-python-function) – Ashwini Chaudhary Oct 22 '13 at 13:23
  • Show the rest of the code. – Lasse V. Karlsen Oct 22 '13 at 13:25
  • i have posted my code, check now – Sil Oct 22 '13 at 13:40
  • 2
    Please double check your indentation. the `list_of_messages` line is at the same level as the `def` preceding it. Is `main:` part of your code? I think that would cause a syntax error. You don't appear to have any `print` statements anywhere in the code you wrote, so how are you getting output at all? – Kevin Oct 22 '13 at 13:42
  • Kevin no. its not. it did that when i was coping and pasting it. Also, i put print statement in it but took it out to show you guys my code. – Sil Oct 22 '13 at 14:44

3 Answers3

1

As Kevin pointed out, I think this is related to your print function printing (by side effect) and then returning None (as do all Python functions with no explicit return statement).

For instance:

>>> x = 3
>>> print(x)
3
>>> print(print(x))
3
None
val
  • 8,459
  • 30
  • 34
  • True: `def printFile(): f = open("abc.txt") lines = f.readlines() for line in lines: print line` And calling it as: `print printFile()` was giving me `None` at the end. After I changed to just `printFile()` it didn't print the extra `None` – Hunterr Aug 30 '17 at 11:18
0

The third line is blank? So the file ends with a newline then nothing else? That's exactly the output you've shown! If you want a third entry in the array, then add a space to the blank line in the file. Not sure why you'd want that though.

What are you trying to achieve with your code? Including the code you haven't shown us...

hcarver
  • 7,126
  • 4
  • 41
  • 67
  • The questioner is confused about the mystery `None`, not the contents of the list. – Tim Oct 22 '13 at 13:24
0

Seems like you have an error there. You must have omitted the final line in your text editor, because with the same input you've specified, it should output with the following behaviour.

with open("test.txt", "r") as f:
    f.readlines()

Outputs:

['this is the first line\n', 'this is the second line\n', '\n']
Megatron
  • 15,909
  • 12
  • 89
  • 97
  • It doesn't output that and it doesn't print, capture, or save the result of calling `f.readlines()` -- but you're right about that being what the return value would be. – martineau Oct 22 '13 at 13:28