tags:

views:

78

answers:

5

The code below is used in a function:

def print_query(x):
    h = open('/home/rv/data.txt', 'r')
    read = h.readlines()
    for line in read:
        return line

When the value "line" is retunred it should print but instead i get the value "None"

+2  A: 

You are not checking if the "read" variable actually contains any lines - if it does not, then the function will fall through the for loop and return None.

Using the for loop is also silly - why would you read all lines, and only return the first one, especially in a for loop? What happens when the file can't be opened?

Yann Ramin
I'm pretty sure that the loop will only execute one time, but it is still a bizarre anti-pattern for getting the first line of a file. There about to be a contest to find stranger ways of doing it than this.
Adam Crossland
the file isnt empty, ah i didnt relise how would i retuen all the lines?
Liam
Do you want to return the line to the calling code or just print them?If you want to return them, just return h.readlines()
Adam Crossland
I tried return h.readlines(), but i get [] as the output
Liam
That's because your file is empty
Josh Wright
@Liam, the file `/home/rv/data.txt` really is empty. Make sure you are using the correct filename. Check again that it has something in it.
gnibbler
+8  A: 

Try this:

with open('/home/rv/data.txt','r') as fh:
    for line in fh:
        print line

If you're on Python 2.5 you might need a from __future__ import with_statement on top.

Also: why do you return the line when you want to print it?

ChristopheD
Looks to me like you have sussed-out OP's actual intention and delivered sane Python code to implement it.
Adam Crossland
+1 for incredibly simple and effective code.
Brian
+1  A: 

I'm not sure why you're bothering with a loop in your function, since it's just going to return after the first iteration anyway. What exactly are you trying to accomplish? It seems like there is a deeper conceptual issue here, beyond the simple implementation issues.

Josh Wright
A: 

When you are writing:

def f:
  return 1
  return 2

the function returns 1.

if you want to return few values you can return it in a list:

def f:
  ans = []
  ans.append(1)
  ans.append(2)
  return ans

another option is to use "yield". google it when you will ready for it

adsurbum
A: 

Another problem with the function is that you're iterating over the wrong object being more verbose than you need to be. To iterate over lines in a file, just do this:

for line in open(file, "r"):
    print line
Brendan Long
readlines() returns a list containing all the lines in the file. While it's obviously more verbose than necessary (and will use a little more memory), there's nothing "wrong" about using readlines to explicitly get the list of lines to iterate over.
Josh Wright
readlines() and later xreadlines() were frequently used in the old days before file objects were iterable
gnibbler
Oh, my mistake. I'll leave this here as it's still somewhat helpful.
Brendan Long