python - 获取指针而不是字符串

标签 python file pointers filesystems

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

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/

相关文章:

python - OpenAI API 错误 : "AttributeError: module ' openai' has no attribute 'Embedding' "

Python:创建一个可以循环迭代的类

php - 手动设置 $_FILES 数组并使用 move_uploaded_file 函数

python - 为什么使用MinHash分析器的查询无法检索重复项?

python - 如何检查列表是否为空?

php - 使用 php 读取 csv 文件的最简单方法,然后选择一个特定值

asp.net-mvc - 获取文件夹中的文件

c# - 指向非托管代码的 Int 指针

c - 在函数中初始化动态数组的值(C语言)

c++ - SymSpellPlusPlus 中的分词