Python基础-day08-文件的操作

  • 文件的基本操作:打开,读取,关闭

打开文件open(参数1,参数2,encoding)

参数1:文件名

参数2:读取模式

f=open("a.txt",mode="r",encoding="utf-8")
content=f.read() //读取所有内容
firstline=f.readline()//读取第一行
print(content) f.close()

 

 

  •    指定路径读取
p=open(r"D:\python27\day01-Helloword\HelloWorld.py",mode="r",encoding="utf-8")
print(p.readlines())
  • 文件模式

r  -读取

a -追加

w-覆盖写入

  • with语句,不用关闭文件
with open("a.txt",mode="r",encoding="utf-8") as f:
    print(f.read())

 

posted @ 2021-07-07 15:42  坚持哪怕一点点  阅读(41)  评论(0编辑  收藏  举报