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:python27day01-HellowordHelloWorld.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())
原文地址:https://www.cnblogs.com/majinjin/p/14981791.html