天天看点

kobe - 学习读取和写入文件

# 读取文件内容
# 打开  open
# 读取  read

# f = open("test.txt", 'r')
# content = f.read()
# print(content)
# # 关闭  close
# f.close()


# # 打开文件 open(文件名,访问模式) 访问模式不写会默认以只读模式
# f = open("test.txt",'w')
# # 写入内容 write
# f.write("刺激战场已经凉了")
# # 关闭文件
# f.close()

# f = open("test.txt",'r+')
# content = f.read()
# print(content)
# # 写入内容 write
# f.write("刺激战场已经凉了")
# # 关闭文件
# f.close()

# 'w' 除了有写入功能,还有新建文件的功能
# f = open("我是你爸爸.txt", 'w')
# f.close()


# 让电脑自动给我们关闭
# with 和,用
# 建模
with open("我是你爸爸.txt", 'r+') as f1, open("我是王者.txt", 'r+') as f2:
    f2.write(f1.read())

# dota2