读写文件

#无需导入包只需以下代码    with open 会自动关闭文件close()     路径于正常路径是反着的  电脑中D:\re.txt
path = "D:/re.txt"
with open(path,"r") as f:
str= f.read()
print(str)
=====================================
等同于
path = "D:/re.txt"
f = open(path,'r')
ff = f.read()
#想着关闭 with open 中是自动关闭
f.close()
print(ff)
-------------------------------------------------
#写入内容会覆盖掉全来文件中的全部内容
with open(path,"w") as f:
  f.write("helloworldlddddddddd")
#with open 用完文件就关闭了 要读取得再打开
fff=open(path)
ff = fff.read()
print(ff)
=======================================

posted @ 2019-02-24 11:28  21keya  阅读(88)  评论(0)    收藏  举报