‘list‘ object has no attribute ‘write‘
时间: 2023-08-11 17:03:57 浏览: 106
It seems like you are trying to call the `write` method on a `list` object. However, the `write` method is not available for lists because it is specifically designed for file objects or objects that support writing data.
If you want to write data to a file, you need to open a file in write mode using the `open` function, and then call the `write` method on the file object. Here's an example:
```python
data = ['apple', 'banana', 'cherry']
# Open the file in write mode
file = open('output.txt', 'w')
# Write the data into the file
file.write('\n'.join(data))
# Close the file
file.close()
```
In this example, the `write` method is called on the file object (`file`), not on the list object (`data`). The `'\n'.join(data)` part is used to join the elements of the list with newline characters before writing them to the file.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![chm](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)