day8:文件操作

1,只读方式打开文件,绝对路径,打开文件,操作文件,关闭文件,file=和 mode= 可以去掉

f = open(file="/Users/guolixiao/Desktop/lisa.txt",mode="r")
content = f.read()
print(content)
f.close #别忘了关闭文件
# 编码方式,我理解的是OS决定的,默认都是UTF-8,但是Windows默认是GBK,如果应用软件指定了,那就按照应用软件的来,没指定按照操作系统默认的 # 不知道对不对 # 绝对路径打开 # mode参数本来就是第二个,可以按照位置参数来打开,但是指定编码的是第四个,必须按照关键字参数打开 # pycharm 默认编码是UTF-8,字符串在内存里面默认是Ascii码,pycharm自动给进行了转化,但是文件这种,需要人为指定 # Mac的linux默认是UTF-8,也可以不指定,但是Windows默认是GBK必须指定 # 文件如果保存成GBK,打开方式也是GBK,那么pycharm软件也得改成GBK,不然还是现实乱码,因为pycharm会用UTF-8去解码GBK, # 也就是你虽然用GBK打开了,但是你读的时候,没有用GBK去解码,而是用的UTF-8 运行结果: my name is lisa,I am 22 years old

2,文件打开模式,r,r+,r+b,w,w+,w+b,a,a+,a+b,其中r和r+用的最多,推荐

3,先说读模式相对路径打开

f = open("../link/lisa2","r")
content = f.read()
print(content)
f.close #别忘了关闭文件
运行结果:
hello,everyone

4,存储的读取的编码方式不一样,会出现乱码,以什么方式存储的文件,就要以什么方式打开操作

f = open("../link/lisa2","r",encoding="GBK")
content = f.read()
print(content)
f.close #别忘了关闭文件
运行结果:
浣犲ソ锛屽ぇ瀹跺ソ

5,rb方式打开,以 bytes方式打开,不可以指定编码方式了,一般这种打开一些非文字文件,例如:图片,视频,音频等

f = open("../link/lisa2","rb")
content = f.read()
print(content)
f.close #别忘了关闭文件
运行结果:
b'xe4xbdxa0xe5xa5xbdxefxbcx8cxe5xa4xa7xe5xaexb6xe5xa5xbd'

6,用r这种方式读出来的内容是str类型的,rb这种是bytes类型的

f = open("../link/lisa2","rb")
content = f.read()
print(content,type(content))
f.close()

f = open("../link/lisa2","r")
content = f.read()
print(content,type(content))
f.close()

运行结果:
b'xe4xbdxa0xe5xa5xbdxefxbcx8cxe5xa4xa7xe5xaexb6xe5xa5xbd' <class 'bytes'>
你好,大家好 <class 'str'>

7,写模式w,注意如果以写的模式打开,那么文件存在,清空写,如果文件不存在,创建文件,清空写,很霸道的模式,所以也是创建文件的一种方式

f = open('file_log.txt',"w",encoding="UTF-8")
f.write("hellohellohellohello")
f.close()
# UTF-8 不区分大小写,都可以

8,wb模式写,注意写之前的文字要编码在写,不然写不进去

f = open('file_log.txt',"wb")
s ="hellohellohellohello"
f.write(s.encode())
f.close()

9,追加模式写,追加模式不会清空原有文件,会在后面写,和上面的写一样,a模式windows下别忘了加encoding,ab模式写之前要编码

10,默认只读打开不可以写,只写打开不可以读

11,读写的话,需要用到加模式 先说r+

f = open('file_log.txt',"r+")
print(f.read())
f.write("world")
print(f.read())
f.close()

运行结果:
hellohellohellohello
运行完之后文件里面内容:
hellohellohellohelloworld
运行前文件内容:
hellohellohellohello 分析:第一次打印完,指针到了最后,所以在最后写了world,然而这时候指针到了最后,第二句打印就没有什么可以打印的了,read和write完全是根据指针来的

12,来看另外一个r+的示例,r+b 比较简单不再掩饰

# 先写后读会覆盖,先读后写会追加
f = open('file_log.txt',"r+")
f.write("world")
print(f.read())
f.close()
运行结果:
hellohellohello
运行完之后文件里面内容:
worldhellohellohello
运行前文件内容:
hellohellohellohello

打印永远是从当前指针开始

13,由于每次写之后,指针位置变了,read不到全部的文件,所以有了指针设定函数seek

f = open('file_log.txt',"r+")
f.write("world")
f.seek(0)
print(f.read())
f.close()

 运行结果:
worldhellohellohello

打印出了所有文件

14,read 函数是按照字符来取得,seek函数是按照字节设置的

f = open('file_log.txt',"r+")
f.seek(2)
print(f.read())
f.close()

文件内容:
大家好,很高兴认识大家

运行结果:
Traceback (most recent call last):
  File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/8.1_文件操作.py", line 61, in <module>
    print(f.read())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa7 in position 0: invalid start byte

分析:
由于文件内容是中文,每个汉字占用3个字节,所以seek设置必须为3的倍数

15,另外一个seek的例子

f = open('file_log.txt',"r+")
f.seek(3)
print(f.read(3))
f.close()

运行结果:
家好,

seek设置为3,指针往后走了1个汉字,但是read参数3得话,往后走了3个汉字

16,获取光标位置tell函数

f = open('file_log.txt',"r+")
f.seek(3)
print(f.tell())
f.close()

运行结果:
3

17,

# 打印最后3的字节,也就是一个汉字, 先读出来指针位置,再设置指针,然后再读
f = open('file_log.txt',"a+")
f.write('你们好')
count =f.tell()
print(count)
content = f.seek(count-3)
print(f.read())
f.close()

运行结果:
51
好

 18,readable,writable函数,判断文件是否可读可写,用的不多,了解即可

 19,文件一下子全部读进来,如果文件比较大的话,可能会造成系统崩溃,所以我们可以用readline来读,非文字的可以用size比如1024来循环读

20,readline 和readlines 的区别

f = open('file_log.txt',"r+")
content=f.readline()
print(content)  # 不循环只读一行
f.close()

f = open('file_log.txt',"r+")
content=f.readlines() #读成了列表
print(content) 
f.close()

运行结果:
我们好

['我们好
', '你们好
', '他们好
', '大家都好
']

21,截取文件的一段truncate函数

f = open('file_log.txt',"r+")
f.truncate(9)
f.close()

运行完之后,文件里面只剩下最前面的三个汉字了

22,for循环可以readline

f = open('file_log.txt',"r+")
for line in f:   # 注意是f 不是f.readline,第二,文件里面已经有一个换行符号了,print还会再加一个
    print(line)
f.close()

运行结果:
我们好

你们好

他们好

大家都好
f = open('file_log.txt',"r+")
for line in f:   # 注意是f 不是f.readline,第二,文件里面已经有一个换行符号了,print还会再加一个
    print(line,end="")
f.close()

运行结果:
我们好
你们好
他们好
大家都好

23,由于每次打开文件,都要关闭,如果嫌弃麻烦,可以用with open方式打开,还可以一次打开多个文件,推荐使用,一般as 后面的名字是obj,或者f 啥啥的

with open('file_log.txt',"r+") as f1, open("lisa.txt","w+") as f2:
    print(f1.read())
    print("-----------------")
    f2.write("hellllllll")
    f2.seek(0)
    print(f2.read())

运行结果:
我们好
你们好
他们好
大家都好
-----------------
hellllllll
with open('file_log.txt',"r+") as f1, 
        open("lisa.txt","w+") as f2:
    print(f1.read())
    print("-----------------")
    f2.write("hellllllll")
    f2.seek(0)
    print(f2.read())

24,最后一个示例,用户名密码,三次错误,则拒绝登录

# 注册创建用户,并储存在文件中,至于什么strip特殊字符啥的,这里先不考虑了
usrname = input("please input username:")
pwd = input("please input password:")
with open("usr.txt","w",encoding="utf-8") as obj:
    # obj.write(usrname,pwd)    # 每次只可以加一个参数,错误,我们想显示成lisa换行123
    # obj.write(usrname)
    # obj.write("
")
    # obj.write(pwd)   # 中间还要单独加个空格,三行代码,麻烦,我们有更简单的一行搞定,format函数
    obj.write("{}
{}".format(usrname,pwd))
# 注册完成了,下面开始登陆的逻辑了
i = 0
while i < 3:
    usr = input("login username:")
    pwd = input("login password:")
    with open("usr.txt","r") as obj:
        li = obj.readlines()
        if li[0].strip() == usr and li[1].strip()==pwd:
            print("success")
            break
        else:
            print("wrong account")
    i = i + 1
#   注意strip函数可以把换行符号也strip掉的
原文地址:https://www.cnblogs.com/lisa-blog/p/10036315.html