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()
2.使用with方法写入:
with open("1111.txt","w",encoding="utf-8") as f: f.write("hello, world")
以上是python保存文件常用的方法,有更好的方法欢迎前来补充。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216382.html原文链接:https://javaforall.net
