Python文件,,1.文件读模式 rf


1.文件读模式 r

f = open("helloworld", ‘r‘, encoding="utf-8")

文件句柄:

"helloworld"表示读的文件的文件名,

‘r‘代表读模式,

encoding="utf-8" 表示字符编码形式为utf-8。

有open就有close,不管是读文件还是写文件,最后一定要关闭文件,f.close()

f = open("helloworld", ‘r‘, encoding="utf-8")s1 = f.read()print(s1)f.close()#输出Hello World!
f = open("helloworld", ‘r‘, encoding="utf-8")s1 = f.read()s2 = f.read()  # 文件读完一遍就没了print(s1)print(s2)  # 所以s1后面就没了,什么都没有f.close()#输出Hello World!

2.文件写模式 w

f = open("helloworld", ‘w‘, encoding="utf-8")

文件句柄:

"helloworld"表示写的文件的文件名,这里的helloworld上面已经读过了,表示已经存在,那么这里写模式还是在helloworld文件里写,那么原来的内容则被现在写的文件内容替换,并不会在原来的内容下面接着写。当然如果文件名是不存在的文件,比如说"helloworld2",那么会自动(可以不手动创建)帮我们新建一个文件名为helloworld2的文件,并且将写的内容添加在文件里。

‘w‘代表读模式,

encoding="utf-8"表示字符编码形式为utf-8。

需要注意的是这里是写 w 模式,只能写f.write(),不能写完之后再进行读文件f.read();w就只能是写,r就只能是读

f = open("helloworld", ‘w‘, encoding="utf-8")f.write("Python\n")f.write("hello world")f.close()#运行之后helloworld文件里内容为Pythonhello world#而原本的helloworld文件里的内容是Hello World!

3.既能读又能写 r+模式

f = open("helloworld2", ‘r+‘, encoding="utf-8")

文件句柄:

"helloworld2"表示读写的文件的文件名,

‘r+‘代表读写模式,可以追加

encoding="utf-8"表示字符编码形式为utf-8。

helloworld2文件里原本的内容为:

Hello WorldPythonwllluyueshenyuqianjiangxiaoxiawangrongshiyuting
f = open("helloworld2", ‘r+‘, encoding="utf-8")print(f.readline().strip())print(f.readline().strip())print(f.readline().strip())f.write("--------------------------")print(f.readline().strip())#输出Hello WorldPythonwllluyue#helloworld2文件里的内容现在变为Hello WorldPythonwllluyueshenyuqianjiangxiaoxiawangrongshiyuting--------------------------

补充:代码里的f.readline()是指一行一行地读文件,第一个f.readline()就是读文件的第一行,所以就输出Hello World,第二个就是读第二行,以此类推下去,我们又写了一行“----------------”,但结果是追加在了最后,而不是在第三行后面,是因为 r+ 模式就是写的内容追加在最后,就这么规定的没办法。.strip()是把空格和换行都去掉,好看一点而已。

4.既能读又能写 w+模式

f = open("helloworld2", ‘w+‘, encoding="utf-8")

文件句柄:

"helloworld2"表示写读的文件的文件名,

‘w+‘代表写读模式,没啥用,不能追加,这里的helloworld2已经存在,所以写读之后写的内容会覆盖原来helloworld2文件里原来的内容。

encoding="utf-8"表示字符编码形式为utf-8。

f = open("helloworld2", ‘w+‘, encoding="utf-8")  # w+是写读模式,没啥用f.write("Hello World\n")f.write("Python\n")f.write("wll\n")f.write("ly\n")print(f.tell())  #tell()字符的计数f.seek(5)  # 光标指到第五个字符这里,其实也不好使,没有办法在指定的光标这里写,只能写后面print(f.readline())  #读一行的第五个字符后面的内容,readline只读一行f.write("jiang\n")  #这里的内容只能写在最后f.close()#输出30 World

5.二进制文件 读 rb模式

f = open("helloworld2", ‘rb‘)

文件句柄:

"helloworld2"表示读的文件的文件名,

"rb" 代表二进制文件读的模式

用到rb模式有两种情况,一是网络传输只能用二进制格式,二是二进制文件必须用二进制打开

f = open("helloworld2", ‘rb‘)print(f.readline())print(f.readline())f.close()#输出b‘Hello World\r\n‘b‘Python\r\n‘#b表示字节,byte类型

6.二进制文件 写 wb模式

f = open("helloworld", ‘wb‘)

文件句柄:

"helloworld"表示读的文件的文件名,

"wb" 代表二进制文件写的模式

f = open("helloworld", ‘wb‘)f.write("hello Tom\n".encode())f.close()#helloworld文件原本的内容是Hello Word!#运行之后helloworld文件的内容变为hello Tom

7.写 a模式

f = open("heloworld2", ‘a‘, encoding="utf-8")

文件句柄:

"helloworld2"表示写读的文件的文件名,

‘a‘ 代表append追加,不覆盖原来的文件了,继续往后写,但是还是不能读文件

encoding="utf-8"表示字符编码形式为utf-8。

f = open("helloworld2", ‘a‘, encoding="utf-8")f.write("syq\n")f.write("jxx\n")f.close()#helloworld2原来的内容为Hello WorldPythonwlllyjiang#执行之后的内容为Hello WorldPythonwlllyjiangsyqjxx

8.a+ 追加读写,但也只能是在后面写

9.读文件

(1)读文件的两种方式

f = open("helloworld2", ‘r‘, encoding="utf-8")s = f.read().strip()print(s)
f = open("helloworld2", ‘r‘, encoding="utf-8")info = f.readlines()for i in info:    print(i.strip())f.close()

(2)读前三行

f = open("helloworld2", ‘r‘, encoding="utf-8")for i in range(3):    print(f.readline().strip())#输出Hello WorldPythonwll

(3)第三行上下分割

f = open("helloworld2", ‘r‘, encoding="utf-8")for index, line in enumerate(f.readlines()):    if index == 3:        print("---------------------")        continue    print(line.strip())#输出Hello WorldPythonwll---------------------jiangsyqjxx
# 高效的循环方法f = open("helloworld2", ‘r‘, encoding="utf-8")count = 0for line in f:    if count == 3:        print("---------------------")        count += 1        continue    print(line.strip())    count += 1#输出Hello WorldPythonwll---------------------jiangsyqjxx

10.文件修改

f = open("helloworld2", "r", encoding="utf-8")f_new = open("helloworld2.bak", "w", encoding="utf-8")for line in f:    if "wll" in line:        line = line.replace("wll", "wulanlan")    f_new.write(line)f.close()f_new.close()#helloworld2内容Hello WorldPythonwlllyjiangsyqjxx#helloworld.bak内容Hello WorldPythonwulanlan  #将wll改成了wulanlanlyjiangsyqjxx

11.f.close()是不是经常忘了写,总是经常忘记关闭,就用with,为了帮你自动关闭文件

with open("helloworld2", "r", encoding="utf-8") as f,         open("helloworld", "r", encoding="utf-8") as f2:  # 打开多个文件,一行不应该超过八十个字符,所以折行写            for line in f:                print(line.strip())#输出Hello WorldPythonwlllyjiangsyqjxx

Python文件

评论关闭