python保存文件常用的两种方式

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://blog.csdn.net/weixin_43215588/article/details/121093281
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞