在我的剧本中,我有以下几点:

file = '%s/data.txt' % (theDirectory)
text = open(file)
theString = text.read
print 'Hello, %s' % (theString)

它返回:
Hello, <built-in method read of file object at 0x100534a48>

这是什么原因?

最佳答案

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

theString = text.read()

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

10-05 21:10