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()把文件内容读出来