python open函数写入文件
时间: 2024-06-08 22:12:35 浏览: 204
Python的open()函数可以用于读取和写入文件。要以写入文件的模式打开一个文件对象,可以在open()函数中传入标示符'w'。例如,可以使用以下代码来打开一个名为example.txt的文件,并将内容写入其中:
```python
file = open('example.txt', 'w')
file.write('This is some text that will be written to the file.')
file.close()
```
在上述代码中,open()函数用于创建一个文件对象,并将其赋值给变量file。然后,可以使用write()方法向文件中写入内容。最后,使用close()方法关闭文件。
阅读全文
相关推荐
















