练习
"""
# 打开文件
file_object = open('log.txt',mode='r',encoding='utf-8') # r,read; w,write; a,append;
# 读取内容
content = file_object.read()
print(content)
# 关闭文件
file_object.close()
"""
"""
# 打开文件
file_object = open('losssg.txt',mode='w',encoding='utf-8') # r,read(只读); w,write(只写,先清空,一般用于新建文件); a,append;
# 写内容
# file_object.write('鬼厉')
# 关闭文件
file_object.close()
"""
"""
# 打开文件
file_object = open('logfffff.txt',mode='a',encoding='utf-8') # r,read(只读); w,write(只写,先清空,一般用于新建文件); a,append;
# 写内容
file_object.write('你好')
# 关闭文件
file_object.close()
"""
读写功能代码
"""
file_object = open('log.txt',mode='w',encoding='utf-8')
file_object.write('asdfadsfasdf\n')
file_object.write('asdfasdfasdfsadf')
file_object.close()
"""
文件可读可写
""""""
"""
读取
写入:根据光标的位置,从当前光标位置开始进行写入操作(可能会将其他的文字覆盖)
"""
"""
file_object = open('log.txt',mode='r+',encoding='utf-8')
# file_object.seek(2) # 调整光标的位置
content = file_object.read()
file_object.write('浪')
# # 读取内容
# content = file_object.read()
# print(content)
#
# file_object.write('666')
# 关闭文件
file_object.close()
"""
"""
file_object = open('log.txt',mode='w+',encoding='utf-8')
data = file_object.read()
print(data)
file_object.write('alex')
file_object.seek(0)
data = file_object.read()
print(data)
file_object.close()
"""
file_object = open('log.txt',mode='a+',encoding='utf-8')
file_object.seek(0)
file_object.write('666')
file_object.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)