python怎么将字符串写入指定文件
时间: 2023-12-20 17:07:37 浏览: 95
可以使用Python中的文件操作函数open()和write()来将字符串写入指定文件中。
首先使用open()函数打开文件,并指定文件名和打开模式(如“w”表示写模式):
```python
file = open("filename.txt", "w")
```
然后使用write()函数将字符串写入文件:
```python
file.write("This is a string to be written in the file.")
```
最后记得要关闭文件:
```python
file.close()
```
完整的代码如下:
```python
file = open("filename.txt", "w")
file.write("This is a string to be written in the file.")
file.close()
```
这样就可以将字符串写入指定文件中了。
阅读全文
相关推荐














