获取指针而不是字符串

tkb*_*kbx 2 python filesystems pointers file

在我的脚本中,我有以下内容:

file = '%s/data.txt' % (theDirectory)
text = open(file)
theString = text.read
print 'Hello, %s' % (theString)
Run Code Online (Sandbox Code Playgroud)

它返回:

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

是什么导致了这个?

Gre*_*ill 6

您需要使用括号调用该方法:

theString = text.read()
Run Code Online (Sandbox Code Playgroud)

如果没有括号,Python会将方法本身的引用赋予theString(此时根本不是字符串).