1.使用open()和close()进行写入:
# 将文件保存到本地
# 1.创建一个文件
create_file = open("demo.txt", "w", encoding="utf-8")
# 2.添加保存内容
data = "hello world"
# 3.写入对应文件
create_file.write(data)
# 4.关闭文件
create_file.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.使用with方法写入:
with open("1111.txt","w",encoding="utf-8") as f:
f.write("hello, world")
- 1
- 2
以上是python保存文件常用的方法,有更好的方法欢迎前来补充。