python怎么创建txt文件
时间: 2023-10-14 14:27:35 浏览: 214
你可以使用Python内置的`open()`函数来创建txt文件。以下是一个简单的示例代码:
```python
# 打开文件,如果文件不存在则创建
file = open("example.txt", "w")
# 写入数据
file.write("Hello, world!")
# 关闭文件
file.close()
```
在上面的代码中,我们首先使用`open()`函数来打开一个名为`example.txt`的文件,使用"w"参数表示以写入模式打开文件,如果文件不存在则会创建该文件。然后,我们使用`write()`方法向文件中写入数据,最后使用`close()`方法关闭文件。
你可以根据自己的需要修改文件名和写入的数据。
阅读全文
相关推荐












