Python之 文件操作

一、文件操作基本流程。

 计算机系统分为:计算机硬件,操作系统,应用程序三部分。

我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知,应用程序是无法直接操作硬件的,这就用到了操作系统。操作系统把复杂的硬件操作封装成简单的接口给用户/应用程序使用,其中文件就是操作系统提供给应用程序来操作硬盘虚拟概念,用户或应用程序通过操作文件,可以将自己的数据永久保存下来。

有了文件的概念,我们无需再去考虑操作硬盘的细节,只需要关注操作文件的流程:

1 #文章.txt
2 #    1,文件路径
3 #    2,编码方式:gbk,utf-8
4 #    3,打开方式:只读,只写,读写,....
5 #    ps:
6 #        错误分析:1 unicodedecodeerror:文件存储和打开的编码不一致.
7 #                 UnicodeDecodeError: 'gbk' codec can't decode byte #0x80 in position 18: illegal multibyte sequence
8 #                转义:路径前面+ r 或者是 双\
9 #                SyntaxError: (unicode error) 'unicodeescape' codec #can't decode bytes in position 2-3: truncated UXXXXXXXX #escape

二、文件编码

f=open(...)是由操作系统打开文件

 f = open('a.txt','r',encoding='utf-8') 

三、文件的打开方式

文件句柄 = open(‘文件路径’,‘模式’)

1.1读

a.只读

 1 # r
 2 #rb一般用在非文字类型的文件:图片,视频
 3 #文件的下载和上传的功能用b模式
 4 f = open('log','r',encoding='utf-8')
 5 content = f.read()
 6 print(content,type(content))
 7 f.close()
 8 #r 五种模式:
 9 #1 f.read() 全部读出来
10 #5,f.read(n)
11 f = open('log',mode='r',encoding='utf-8')
12 content = f.read(3) # r 模式:n 是按照字符读取
13 print(content)
14 f.close()
15 
16 f = open('log',mode='rb')
17 content = f.read(5) # rb 模式:n 是按照字节读取
18 print(content)
19 f.close()
20 # bytes ---> str
21 s = b'xe4xb8xb0xe5x8e'.decode('utf-8')
22 print(s)
23 
24 
25 #2 f.readline() 按行读
26 f = open('log',encoding='utf-8')
27 line = f.readline()
28 print(line)
29 line1 = f.readline()
30 print(line1)
31 f.close()
32 
33 #3 f.readlines() 每一行作为一个元素,放在列表中
34 f = open('log',encoding='utf-8')
35 lines = f.readlines()
36 print(lines)
37 f.close()
38 #4 推荐方式 循环读取
39 f = open('log',encoding='utf-8')
40 for i in f:
41     print(i)
42 f.close()
View Code

b.读写

1 # r+     r+b 一定要注意顺序,先读后写
2 f = open('log','r+',encoding='utf-8')
3 print(f.read())
4 f.write('fdsaf')
5 f.close()
View Code

1.2 写

a.只写

 1 # w 如果没有文件,则创建文件写内容,
 2 #   如果有文件则将原文件内容全部删除,在写
 3 f = open('log','w',encoding='utf-8')
 4 f.write('Alex是sb')
 5 f.close()
 6 f = open('log1','w',encoding='utf-8')
 7 f.write('Alex依然是sb')
 8 f.close()
 9 f = open('log1','wb')
10 f.write('Alex依然是sb'.encode('utf-8'))
11 f.close()
View Code

b.写读

1 #w+  w+b
2 f = open('log','w+',encoding='utf-8')
3 f.write('aaaa')
4 f.seek(0)  #移动光标
5 print(f.read())
6 f.close()
View Code

1.3追加

a.只追加

1 #追加 a ab
2 f = open('log','a',encoding='utf-8')
3 f.write('wusir紧跟其后')
4 f.close()
View Code

b.追加可读

1 f = open('log','a+',encoding='utf-8')
2 f.write('aaaa')
3 f.tell()
4 print(f.tell())
5 f.seek(2)
6 print(f.readable())
7 print(f.read())
8 f.close()
View Code

 四、文件操作方法

4.1 常用操作方法

read(3):

  1. 文件打开方式为文本模式时,代表读取3个字符

  2. 文件打开方式为b模式时,代表读取3个字节

1 f = open('log',mode='r',encoding='utf-8')
2 content = f.read(3) # r 模式:n 是按照字符读取
3 print(content)
4 f.close()
5 
6 f = open('log',mode='rb')
7 content = f.read(5) # rb 模式:n 是按照字节读取
8 print(content)
9 f.close()

seek:

1 f = open('log',encoding='utf-8')
2 f.seek(3)  #按照字节调整
3 print(f.read())
4 f.close()

truncate:

1 f = open('log','a',encoding='utf-8')
2 f.truncate(3)  #按字节截取 截取前面的内容
3 f.close()

readline:

 1 f = open('log',encoding='utf-8')
 2 line = f.readline()
 3 print(line)
 4 line1 = f.readline()
 5 print(line1)
 6 f.close()
 7 f = open('log',encoding='utf-8')
 8 lines = f.readlines()
 9 print(lines)
10 f.close()

五、文件修改

#改动文件:
#1,创建一个新文件.
#2,读取原文件.
#3,将原文件的内容通过你想要的方式进行更改,并写入新文件件.
#4,将原文件删除.
import os
with open('log',encoding='utf-8') as f1,
    open('log.bak','w',encoding='utf-8') as f2:
    for i in f1:
        i = i.replace('alex','SB')
        f2.write(i)
os.remove('log')
os.rename('log.bak','log')
原文地址:https://www.cnblogs.com/xiaobin12126/p/8385094.html