python的一个print问题

python代码为:
with open ('test.txt') as test:
print(test)

test的内容为
1:test1
2:test2
3:test3

为什么结果显示为
<_io.TextIOWrapper name='test.txt' mode='r' encoding='cp936'>

阅读 2.9k
4 个回答
with open('test.txt', 'r') as test:
    print(test.read())

你得调用read方法才能读出来内容呢把。这样只是读了个文件对象把?

with open ('test.txt') as test:
    for i in test.readlines():
        print(i)
for line in open(file_path):
    print(line.strip())
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题