'_io.TextIOWrapper' object has no attribute 'apend' File "C:\Users\lenovo\Desktop\实习\python\#输出测试.py", line 48, in <module> new_file.apend(info)
时间: 2023-07-21 17:03:52 浏览: 51
你遇到的错误是因为在文件对象上使用了错误的方法。正确的方法是`append()`而不是`apend()`。
请将代码中的`apend()`改为`append()`,如下所示:
```python
new_file.append(info)
```
这样就可以将`info`写入到`new_file`文件对象中了。请记得在使用`append()`方法之前,确保已经正确地打开了文件并获得了文件对象。
相关问题
_io.TextIOWrapper object has no attribute describe
这个错误提示是因为 `TextIOWrapper` 类没有 `describe` 属性。`TextIOWrapper` 是 Python 标准库中用于将字节流转换为文本流的类,它包装了一个底层的字节流对象,提供了一个类文件对象的接口。如果您想要描述一个文件对象的属性,可以使用 `os.stat()` 或 `os.fstat()` 函数来获取文件的元数据信息。例如,您可以使用以下代码来获取文件的大小和修改时间:
```python
import os
with open('file.txt', 'r') as f:
statinfo = os.fstat(f.fileno())
print(statinfo.st_size) # 文件大小
print(statinfo.st_mtime) # 修改时间
```
'_io.TextIOWrapper' object has no attribute 'append' File "C:\Users\lenovo\Desktop\实习\python\#输出测试.py", line 48, in <module> new_file.append(info) ^^^^^^^^^^^^^^^ AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
很抱歉,我之前的回答有误。在 Python 中,文件对象是没有 `append()` 方法的,这是列表对象的方法。
如果你想要将内容追加到文件中,正确的方法是使用文件对象的 `write()` 方法。你可以使用 `with open() as` 语句打开文件,然后使用文件对象的 `write()` 方法将内容写入文件。示例如下:
```python
with open('file.txt', 'a') as file:
file.write(info)
```
在这个例子中,我们使用 `open()` 函数以追加模式 `'a'` 打开文件,并将文件对象赋值给 `file` 变量。然后,我们使用 `file.write(info)` 将 `info` 写入文件中。
请注意,在使用 `write()` 方法时,你需要确保 `info` 是一个字符串类型。如果不是字符串类型,你需要先将其转换为字符串。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)