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方法写入:w
·
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保存文件常用的方法,有更好的方法欢迎前来补充。

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)