013.Python的文件操作

一 文件操作

  • fp = open("打开的文件",mode="模式选择",encoding="编码集")
  • open 函数 返回一个文件io对象 (别名:文件句柄)
  • i => input 输入
  • o => output 输出

1.1 写入文件内容

#打开文件
fp = open("0208.txt",mode="w",encoding="utf-8")
#写入内容
fp.write("今天是个好日子")
#关闭文件
fp.close()

执行并查看

[root@node10 python]# python3 test.py
[root@node10 python]# ll
total 8
-rw-r--r--. 1 root root 21 Nov 24 03:50 0208.txt
-rw-r--r--. 1 root root 93 Nov 24 03:50 test.py
[root@node10 python]# cat 0208.txt
今天是个好日子[

1.2 读取文件内容

#打开文件
fp = open("0208.txt",mode="r",encoding="utf-8")
#读取内容
res = fp.read()
#关闭文件
fp.close()
#打印值
print(res)

bytes模式

1.3 二进制的字节流

  • 一堆字符放在一起 是字符串
  • 一堆字节放在一起 是字节流
  • 字节流用来传输数据,用来保存数据

在ascii编码字符前加上b ,代表二进制字节流,其他所有字符都不能这样加(比如中文是不行的)

strvar = b'123'
print(strvar,type(strvar)) #b'123' <class 'bytes'>

# 使用encode 和 decode 来把中文转化成二进制字节流
# encode 把中文变成字节流 (编码)
strvar = "非诚勿扰"
res = strvar.encode("utf-8")
print(res)
# decode 把字节流变成中文 (解码)
strvar = res.decode("utf-8")
print(strvar)

执行

[root@node10 python]# python3 test.py
b'123' <class 'bytes'>
b'xe9x9dx9exe8xafx9axe5x8bxbfxe6x89xb0'
非诚勿扰

1.4 复制图片

实际就是把图片中的二进制字节流拷贝相互来,放到另一个文件当中

二进制的字节模式,不要指定编码集是utf-8

#读取文件的内容
fp = open("集合.png",mode="rb") res = fp.read() fp.close() # [写入]另外一个文件中 fp = open("集合2.png",mode="wb") res = fp.write(res) fp.close()

执行

-rw-r--r--. 1 root root    22 Nov 24 03:54 0208.txt
-rw-r--r--. 1 root root   160 Nov 24 04:19 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# rm -rf 集合2.png
[root@node10 python]# ll
total 32
-rw-r--r--. 1 root root    22 Nov 24 03:54 0208.txt
-rw-r--r--. 1 root root   160 Nov 24 04:19 test.py
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# python3 test.py
[root@node10 python]# ll
total 56
-rw-r--r--. 1 root root    22 Nov 24 03:54 0208.txt
-rw-r--r--. 1 root root   160 Nov 24 04:19 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png

1.5 追加模式

(utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节)
    #read()        功能: 读取字符的个数(里面的参数代表字符个数)
    #seek()        功能: 调整指针的位置(里面的参数代表字节个数)
    #tell()        功能: 当前光标左侧所有的字节数(返回字节数)

1.5.1 r+可读可写(先读后写)

fp = open("0208.txt",mode="r+",encoding="utf-8")
res = fp.read()
print(res)
fp.write("ffzzz")

# 读取所有字符串
fp.seek(0)
res = fp.read()
print(res)
fp.close()

执行

[root@node10 python]# python3 test.py
今天是个好日子

今天是个好日子
ffzzz

1.5.2 r+可读可写,先写后读

fp = open("0208.txt",mode="r+",encoding="utf-8")
# 先把光标移动到文件最后
fp.seek(0,2)
# 在最后追加abcd ,避免覆盖以前原有字符串
fp.write("abcd")
# 使用seek移动光标,把光标移动到文件开头
fp.seek(0)
res = fp.read()
print(res)
fp.close()

执行

[root@node10 python]# python3 test.py
今天是个好日子
ffzzzabcd

1.5.3 w+可读可写(默认光标在文件的开头)

fp = open("0208_1.txt",mode="w+",encoding="utf-8")
fp.write("今天天气下了雨")

# 可读
fp.seek(0)
res = fp.read()
print(res)
fp.close()

执行

[root@node10 python]# python3 test.py
今天天气下了雨
[root@node10 python]# ll
total 60
-rw-r--r--. 1 root root    21 Nov 24 06:40 0208_1.txt
-rw-r--r--. 1 root root    31 Nov 24 06:37 0208.txt
-rw-r--r--. 1 root root   144 Nov 24 06:40 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# cat 0208_1.txt
今天天气下了雨

除了r模式不能够自动创建文件之外,w和a都可以

1.5.4 a+ append 可读可写 (默认光标在文件的结尾) (在写入的时候,只能在末尾强制追加)

fp = open("0208_2.txt",mode="a+",encoding="utf-8")
fp.write("今天晚上要早走")

fp.seek(0)
res = fp.read()
print(res)
fp.close()

执行

[root@node10 python]# python3 test.py
今天晚上要早走
[root@node10 python]# ll
total 64
-rw-r--r--. 1 root root    21 Nov 24 06:40 0208_1.txt
-rw-r--r--. 1 root root    21 Nov 24 06:43 0208_2.txt
-rw-r--r--. 1 root root    31 Nov 24 06:37 0208.txt
-rw-r--r--. 1 root root   135 Nov 24 06:43 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# cat 0208_2.txt
今天晚上要早走

1.6 seek,read,tell语法

fp = open("0208_3.txt",mode="a+",encoding="utf-8")
fp.write("123456789")
res = fp.tell() # 返回当前光标左边所有字符的字节
print(res)
fp.seek(4)       # 移动到第四个字节的位置
res = fp.tell()  # 返回当前光标左侧所有的字节数
print(res)

res = fp.read(2) # read可以在括号里面指定读几个[字符]
res = fp.tell()  # 获取当前光标左侧的字节数
print(res)
fp.close()

执行

[root@node10 python]# python3 test.py
9
4
6
[root@node10 python]# ll
total 68
-rw-r--r--. 1 root root    21 Nov 24 06:40 0208_1.txt
-rw-r--r--. 1 root root    21 Nov 24 06:43 0208_2.txt
-rw-r--r--. 1 root root     9 Nov 24 07:19 0208_3.txt
-rw-r--r--. 1 root root    31 Nov 24 06:37 0208.txt
-rw-r--r--. 1 root root   422 Nov 24 07:19 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# cat 0208_3.txt
123456789

1.7 with语法

with 语法 简化操作  可以省去fp.close() 这句代码,实现自动关闭
# with + open(文件操作) as 为open的返回值起别名  fp 就是名称
# 相当于 fp = open() 一样的

示例

with open("0404_6.txt",mode="a+",encoding="utf-8") as fp:
        fp.write("ceshidaima")
        fp.seek(0) # fp.seek(0,2) 移动到文件的末尾
        res= fp.read()
        print(res)
        # fp.close 这句话 with语法自动帮助我们完成

执行

[root@node10 python]# python3 test.py
ceshidaima
[root@node10 python]# ll
total 72
-rw-r--r--. 1 root root    21 Nov 24 06:40 0208_1.txt
-rw-r--r--. 1 root root    21 Nov 24 06:43 0208_2.txt
-rw-r--r--. 1 root root     9 Nov 24 07:19 0208_3.txt
-rw-r--r--. 1 root root    10 Nov 24 07:26 0208_4.txt
-rw-r--r--. 1 root root    31 Nov 24 06:37 0208.txt
-rw-r--r--. 1 root root   219 Nov 24 07:26 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png
[root@node10 python]# cat 0208_4.txt
ceshidaima

简化文件复制操作  open 之间 可以用逗号,隔开 为了简化操作

with open("集合.png",mode="rb") as fp1,open("集合3.png",mode="wb") as fp2:
        res = fp1.read()
        strvar  = fp2.write(res)

执行

[root@node10 python]# python3 test.py
[root@node10 python]# ll
total 96
-rw-r--r--. 1 root root    21 Nov 24 06:40 0208_1.txt
-rw-r--r--. 1 root root    21 Nov 24 06:43 0208_2.txt
-rw-r--r--. 1 root root     9 Nov 24 07:19 0208_3.txt
-rw-r--r--. 1 root root    10 Nov 24 07:26 0208_4.txt
-rw-r--r--. 1 root root    31 Nov 24 06:37 0208.txt
-rw-r--r--. 1 root root   124 Nov 24 07:27 test.py
-rw-r--r--. 1 root root 21962 Nov 24 06:27 集合2.png
-rw-r--r--. 1 root root 21962 Nov 24 07:27 集合3.png
-rw-r--r--. 1 root root 21962 Nov 24 04:21 集合.png

二 文件操作的相关函数

2.1 刷新缓冲区 flush

  • 当文件关闭的时候自动刷新缓冲区
  • 当整个程序运行结束的时候自动刷新缓冲区
  • 当缓冲区写满了 会自动刷新缓冲区
  • 手动刷新缓冲区
fp = open("0208_5.txt",mode="w+",encoding="utf-8")
fp.write("测试close操作")
 while True:
         pass
fp.close()

执行

[root@node10 python]# python3 test.py

会卡住,但是有文件,内容为空,是因为没有执行closed的操作,内容还没有存盘,另开终端查看:

 使用flush刷新缓冲区

fp = open("0208_5.txt",mode="w+",encoding="utf-8")
fp.write("测试close操作")
fp.flush()
while True:
        pass
fp.close()

执行,冰凌开终端查看

[root@node10 python]# python3 test.py

2.2 readline()

功能: 读取一行文件内容
readline(字符个数) 单位读的时字符的个数
如果当前字符个数 > 实际个数 那就只读当前行
如果当前字符个数 < 实际个数 那就按照给与的当前个数读取 

写有个文件

[root@node10 python]# cat 0208_6.txt

寒蝉凄切,对长亭晚,骤雨初歇。
都门帐饮无绪,留恋处,兰舟催发。
执手相看泪眼,竟无语凝噎。
念去去千里烟波,暮霭沉沉楚天阔。

多情自古伤离别。
更那堪,冷落清秋节。
今宵酒醒何处?
杨柳岸、晓风残月。
此去经年,应是良辰好景虚设。
便纵有千种风情,更与何人说?

示例:

fp = open("0208_6.txt",mode = "r+",encoding="utf-8")
res = fp.readline()
print (res)

执行

[root@node10 python]# python3 test.py
寒蝉凄切,对长亭晚,骤雨初歇。

读字符数

fp = open("0208_6.txt",mode = "r+",encoding="utf-8")
res = fp.readline(3)
print (res)

执行

[root@node10 python]# python3 test.py
寒蝉凄    #只读了前三个

读出所有行数 每一行都读

fp = open("0208_6.txt",mode = "r+",encoding="utf-8")
res = fp.readline()
# 判断res是不是为空,如果不是空,则进去打印该字符串
while res:
        print(res)
        # 在继续向下读取一行
        res = fp.readline() # 读到最后是一个'' bool('') => False

执行

[root@node10 python]# python3 test.py
寒蝉凄切,对长亭晚,骤雨初歇。

都门帐饮无绪,留恋处,兰舟催发。

执手相看泪眼,竟无语凝噎。

念去去千里烟波,暮霭沉沉楚天阔。



多情自古伤离别。

更那堪,冷落清秋节。

今宵酒醒何处?

杨柳岸、晓风残月。

此去经年,应是良辰好景虚设。

便纵有千种风情,更与何人说?
fp = open("0208_6.txt",mode = "r+",encoding="utf-8")
res = fp.readline(3)
# 判断res是不是为空,如果不是空,则进去打印该字符串
while res:
        print(res)
        # 在继续向下读取一行
        res = fp.readline() # 读到最后是一个'' bool('') => False

执行

[root@node10 python]# python3 test.py
寒蝉凄
切,对长亭晚,骤雨初歇。

都门帐饮无绪,留恋处,兰舟催发。

执手相看泪眼,竟无语凝噎。

念去去千里烟波,暮霭沉沉楚天阔。



多情自古伤离别。

更那堪,冷落清秋节。

今宵酒醒何处?

杨柳岸、晓风残月。

此去经年,应是良辰好景虚设。

便纵有千种风情,更与何人说?

2.3 readlines() 

功能:将文件中的内容按照换行读取到列表当中

with open("0208_6.txt",mode = "r+",encoding="utf-8") as fp:
        res = fp.readlines()
        print (res)

执行

[root@node10 python]# python3 test.py
['寒蝉凄切,对长亭晚,骤雨初歇。
', '都门帐饮无绪,留恋处,兰舟催发。
', '执手相看泪眼,竟无语凝噎。
', '念去去千里烟波,暮霭沉沉楚天阔。
', '
', '多情自古伤离别。
', '更那堪,冷落清秋节。
', '今宵酒醒何处?
', '杨柳岸、晓风残月。
', '此去经年,应是良辰好景虚设。
', '便纵有千种风情,更与何人说?
']

写道列表中

listvar = []
with open("0208_6.txt",mode = "r+",encoding="utf-8") as fp:
        res = fp.readlines()
        # 过滤数据
        for i in res:
                # 过滤两边的空白符
                res = i.strip()
                # 把过滤好的数据直接插入到列表当中
                listvar.append(res)
print(listvar)

执行

[root@node10 python]# python3 test.py
['寒蝉凄切,对长亭晚,骤雨初歇。', '都门帐饮无绪,留恋处,兰舟催发。', '执手相看泪眼,竟无语凝噎。', '念去去千里烟波,暮霭沉沉楚天阔。', '', '多情自古伤离别。', '更那堪,冷落清秋节。', '今宵酒醒何处?', '杨柳岸、晓风残月。', '此去经年,应是良辰好景虚设。', '便纵有千种风情,更与何人说?']

2.4 writelines() 

  • 功能:将内容是字符串的可迭代性数据写入文件中
  • 参数:内容为字符串类型的可迭代数据
with open("0208_7.txt",mode = "w+",encoding="utf-8") as fp:
         strvar = "123456"
         fp.writelines(strvar)

执行

[root@node10 python]# python3 test.py
[root@node10 python]# cat 0208_7.txt
12345

列表类型

with open("0208_7.txt",mode = "a+",encoding="utf-8") as fp:
         strvar = ["aaa",'bbb','ccc']
         fp.writelines(strvar)

执行

[root@node10 python]# python3 test.py
[root@node10 python]# cat 0208_7.txt
123456
aaabbbccc[

int的不能写,必须强制格式化

with open("0208_7.txt",mode = "w+",encoding="utf-8") as fp:
        res = str([1,2,3,4,5])
        fp.writelines(res)

执行

[root@node10 python]# python3 test.py
[root@node10 python]# cat 0208_7.txt
[1, 2, 3, 4, 5]

2.5 truncate()

功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)

with open("0208_6.txt",mode="r+",encoding="utf-8") as fp:
        fp.truncate(5)

执行

[root@node10 python]# python3 test.py
[root@node10 python]# cat 0208_6.txt
寒▒

readable()  功能: 判断文件对象是否可读

writable ()     功能: 判断文件对象是否可写

[root@node10 python]# cat test.py
fp = open("0208_6.txt",mode="w",encoding="utf-8")
res1 = fp.readable()
res2 = fp.writable()
print(res1,res2)

执行

[root@node10 python]# python3 test.py
False True

遍历

# 文件对象具有可迭代性  
# 遍历fp对象 和 readline一行一行读取的操作是一样的.
for i in fp:
    print(i)
原文地址:https://www.cnblogs.com/zyxnhr/p/12283545.html