在我的脚本中,我有以下内容:
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
(此时根本不是字符串)。
关于python - 获取指针而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8827485/