Python——详细介绍文件操作那些事儿,open、with等命令统统拿下!

创作不易,来了的客官点点关注,收藏,订阅一键三连❤😜  

目录

字符编码

ASCII码

Unicode

GBK(GB2312)与UTF-8

文件操作

OPEN

打开文件的模式

语法糖管理打开文件对象

with、for

光标移动

关于写文件

为什么不实时写入磁盘

什么时候会写入磁盘


字符编码

翻译机:实现人与机器

ASCII

英文字符映射表 (python2-->#encoding:utf-8,声明使用utf-8)

Unicode

万国码,所有的文字都给予了编号

查看序号:ord( )

编码的具体实现方式:utf-8.utf-16

utf-8  -->可变长的编码方式,一个英文占用1一个字节,一个中文占用三个字节(python3)

GB2312 -->一般中文占2个字节

encode:查看存储的字节,用什么加码就需要用什么类型解码

bytes  <----> str  

ecode <----> str

>>> str1 = "中"

>>> str1.encode("utf-8")

b'\xe4\xb8\xad'

>>> str1.encode("utf-8").decode("utf-8")

'中'

GBKGB2312)与UTF-8

GBK是在国家标准GB2312基础上扩容后兼容GB2312的标准(好像还不是国家标准)。GBK编码专门用来解决中文编码的,是双字节的。不论中英文都是双字节的。

UTF-8 编码是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码。对于英文字符较多的论坛则用UTF-8 节省空间。另外,如果是外国人访问你的GBK网页,需要下载中文语言包支持。访问UTF-8编码的网页则不出现这问题。可以直接访问。

GBK包含全部中文字符;UTF-8则包含全世界所有国家需要用到的字符。


文件操作

OPEN

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file : 要打开的文件名( str )

mode: 打开文件的方式( str ) => text, bytes

encoding: 文件编码方式(str)

errors: 当发生编码错误时的处理方式(str),'ignore'或'strict'(默认)

buffering: 缓存方式 ( int)

eg1:

>>> fp = open("utf.txt")

>>> fp.read()

'这是utf8\n'

>>> fp = open("gbk.txt",encoding="gbk")

>>> fp.read()

'这是gbk'

eg2:打开文件

[root@chaochao ~]# touch ip.txt

[root@chaochao ~]# vim ip.txt

[root@chaochao ~]# python3

Python 3.6.8 (default, Nov 16 2020, 16:55:22)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> fp = open("ip.txt")

>>> fp.read()

'123\n1=2\n3=3\n23\n\n'

eg3:查看文件的各个类型。(rb-->2进制读取)

>>> import chardet

>>> fp = open("utf.txt")

>>> fp = open("utf.txt","rb")

>>> chardet.detect(fp.read())

{'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

>>> fp2 = open("gbk.txt","rb")

>>> chardet.detect(fp2.read())

{'encoding': 'ISO-8859-5', 'confidence': 0.4388398420352105, 'language': 'Russian'}

打开文件的模式

  • fp.flush()  --> 强制刷新

fp.close()  --> 文件连接关闭

  • R:读模式

>>> fp = open("utf.txt","r")

>>> fp.read()

'这是utf8\n'

>>> fp = open("utf.txt","w")

>>> fp.read()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

io.UnsupportedOperation: not readable

  • W:覆盖写入
>>> fp = open("utf.txt","w")

>>> fp.write("chaochao")

8

>>> fp.flush()    --->刷到磁盘(强制刷新)

>>>

[root@chaochao ~]# cat utf.txt

chaochao[root@chaochao ~]# python3

Python 3.6.8 (default, Nov 16 2020, 16:55:22)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux

Type "help", "copyright", "credits" or "license" for more information.
  • a:追加文本
>>> fp = open("utf.txt","a")

>>> fp.write("yyds")

4

>>> fp.flush()

>>>

[root@chaochao ~]# cat utf.txt

chaochaoyyds[root@chaochao ~]#
  • X:
>>> fp = open("yyds.txt","x")

>>> fp.write("1989")

4

>>> fp.flush()

>>>

[root@chaochao ~]# cat yyds.txt

1989[root@chaochao ~]#

语法糖管理打开文件对象

with、for

eg:

>>> with open("utf.txt") as fp:

...     fp.read(1)

...

'y'

>>> fp.read(1)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ValueError: I/O operation on closed file

光标移动

fp.tell() -->文件光标位置,整数

fp.seek(cookie,whence=0) -->一定光标的位置

cookie:偏移量

whence=  -->0(开始)  1(当前) 2(末尾)

fp.readline()  --->按行读取

eg

>>> fp = open("utf.txt")

>>> fp.read(1)

'y'

>>> fp.read(2)

'yd'

>>> fp.tell()

3

>>> fp.seek(10)

10

>>> fp.tell()

10

>>> fp.seek(0)

0

>>> fp.tell()

0

>>> fp.seek(1)

1

>>> fp.tell()

1

fp.readline()

>>> fp.read()

'yyds\nchaochao\n1989\ntaylor\n1233124\n'

>>> fp.readlines(4)

[]

>>> fp.seek(0)

0

>>> fp.readlines(5)

['yyds\n', 'chaochao\n']

>>> fp.seek(0)

0

>>> fp.readlines(15)

['yyds\n', 'chaochao\n', '1989\n']

关于写文件

为什么不实时写入磁盘

硬盘是慢设备,频率读写会增大磁盘压力,产生瓶颈

什么时候会写入磁盘

 f.flush()

 f.close()

 buffer设置(默认:io.DEFAULT_BUFFER_SIZE )

 0 => 实时写入 (binary mode)

 1 => 行缓存 ( text mode) => \n

 其他数字n => 缓冲区大小n

创作不易,客官点个赞吧!评论一下!一起加油❤😜 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chaochao️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值