Python file.read()函数输出?

Mic*_*ael 0 python

我试图使用Python中的File.read()函数将文件的内容输出到终端,但继续接收以下与".txt"文件内容不匹配的输出.

Python代码

from sys import argv
script, input_file = argv

def print_all(f):
    print f.read

current_file = open(input_file)
print "Print File contents:\n"
print_all(current_file)
current_file.close()
Run Code Online (Sandbox Code Playgroud)

输出:

Print File contents:

<built-in method read of file object at 0x1004bd470>
Run Code Online (Sandbox Code Playgroud)

Lev*_*von 5

如果要调用函数,则需要()在函数名称后面(以及任何必需的参数)

因此,在你的函数中print_all替换:

print f.read    # this prints out the object reference
Run Code Online (Sandbox Code Playgroud)

有:

print f.read()  # this calls the function
Run Code Online (Sandbox Code Playgroud)