没有返回任何值

Lia*_*iam 2 python

以下代码用于函数:

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

当值"line"被重新调整时,它应该打印但是我得到值"None"

Chr*_*heD 8

试试这个:

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

如果您使用的是Python 2.5,则可能需要一个from __future__ import with_statement顶部.

另外:为什么return你想要print它的线?