pythpn3.10.7无法调用rstrip

问题遇到的现象和发生背景

pythpn3.10.7无法调用rstrip

用代码块功能插入代码,请勿粘贴截图

with open('pi_digits.txt') as file_dbject:
contents = file_dbject
print(contents.rstrip())

运行结果及报错内容

AttributeError: '_io.TextIOWrapper' object has no attribute 'rstrip'

我的解答思路和尝试过的方法

站内查询

我想要达到的结果

可以正常调用rstrip


with open('pi_digits.txt', 'r') as file_dbject:
    contents = file_dbject.read()
    print(contents.rstrip())


```

1.file_dbject 是文件对象,也就是句柄,虽然你可以用它来迭代出文件内容,但他不是字符串
2.rstrip是字符串的方法,所以你要file_dbject.read()把文件内容读出来