一 文件操作

  • 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'\xe9\x9d\x9e\xe8\xaf\x9a\xe5\x8b\xbf\xe6\x89\xb0'
非诚勿扰

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

写道列表中

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)

013.Python的文件操作的更多相关文章

  1. Python :open文件操作,配合read()使用!

    python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...

  2. Python 常见文件操作的函数示例(转)

    转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...

  3. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  4. 孤荷凌寒自学python第三十三天python的文件操作初识

     孤荷凌寒自学python第三十三天python的文件操作初识 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天开始自学python的普通 文件操作部分的内容. 一.python的文件打开 ...

  5. python中文件操作的六种模式及对文件某一行进行修改的方法

    一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...

  6. python中文件操作的其他方法

    前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...

  7. Python常见文件操作的函数示例

    # -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...

  8. python的文件操作及简单的用例

    一.python的文件操作介绍 1.文件操作函数介绍 open() 打开一个文件 语法:open(file, mode='r', buffering=-1, encoding=None, errors ...

  9. python基本文件操作

    python文件操作 python的文件操作相对于java复杂的IO流简单了好多,只要关心文件的读和写就行了 基本的文件操作 要注意的是,当不存在某路径的文件时,w,a模式会自动新建此文件夹,当读模式 ...

随机推荐

  1. 201871010113-贾荣娟 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告

    项目 内容 课程班级博客链接 18级卓越班 这个作业要求链接 实验三-软件工程结对项目 这个课程学习目标 掌握软件开发流程,提高自身能力 这个作业在哪些方面帮助我实现了学习目标 本次实验让我对软件工程 ...

  2. 16. Vue2.4+新增属性$attrs

    vm.$attrs简介 首先我们来看下vue官方对vm.$attrs的介绍: 包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 style 除外).当一个组件没有声明 ...

  3. Ducci Sequence UVA - 1594

      A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...

  4. Score UVA - 1585

    ​ There is an objective test result such as "OOXXOXXOOO". An 'O' means a correct answer of ...

  5. show engine innodb status 输出结果解读

    show engine innodb status 输出结果解读 基于MySQL 5.7.32 最近想整理一下show engine innodb status的解读,但是发现中文互联网上相关的信息要 ...

  6. SpringBoot的旅游项目——day01(学习记录附赠源码)

    前言 学完SpringBoot的项目,Github地址,欢迎start,一起学习! 第一天 一.技术选型 基于SpringBoot+VUE的前后端分离的仿照马蜂窝的项目. 后端选用的技术为: Spri ...

  7. hdu 3062 基础的2sat

    题意: Party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. LA2678最短子序列

    题意:       给你一个正整数序列,问你在里面找到一个最短的子序列,要求子序列的和大于等于k,输出序列长度. 思路:       这个序列的每个数字都是正整数,那么就比较好想了,我们可以直接枚举终 ...

  9. UVA11248 网络扩容(枚举割边扩充)

    题意:      给你一个有向图,问你从1到n的最大流是多少?如果流量大于等于C那么直接输出一个串,否则输出只扩充一条边的流量就可以达到1->n大于等于C的所有边,如果扩充不了就输出另一个串.S ...

  10. Linux中grep工具的使用

    Grep grep(Globel Search Regular Expression and Printing out the line)全面搜索正则表达式并把行打印出来,是一种强大的文本搜索工具,是 ...