一、路径
绝对路径:
相对路径:
二、读取文件
f = open("./hh.txt","r",encoding="utf_8");
#"./hh.txt" 是相对路径
# r是只读 w是只写
#encoding="utf_8" 编码方式
print(f.read())
#会读取整个文件的内容
print(f.read(10))
#读取10个子节的内容
print(f.readline()) # 只会读取文件一行的内容
print(f.readlines())
#上述调用方法需要关闭文件
f.close()
#这样操作会自动关闭文件
with open(".\hh.txt") as f: # f为自己定义的名字
print(f.open())
三、写文件
with open("hello.txt","w",encoding="utf-8")
#w 为只写,写的过程中会覆盖掉原来的内容
#a 为追加模式,不会覆盖之前的内容
#r+ 支持读写文件,也不会覆盖之前的内容
f.write()