python文件操作(2)

python文件操作(2)

文件的基本操作

一、访问文件的操作过程

  • 打开文件
  • 读取文件
    • 将信息读到内存
  • 写入文件
  • 关闭文件
    • 保存文件并释放内存空间

二、文件的基本操作

1、打开文件(open)

img

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

mode: 打开文件的方式( str )

encoding: 文件编码方式(str)

errors: 当发生编码错误时的处理方式(str)

​ ‘ignore’或’strict’(默认)

buffering: 缓存方式 ( int)

# open('文件路径')
fp = open('gbk.txt')
print(fp, type(fp))
# 读取文件所有内容
print(fp.readlines())
fp.close()

# <_io.TextIOWrapper name='gbk.txt' mode='r' encoding='cp936'> <class '_io.TextIOWrapper'>
# ['abc中国\n', '中国abc']
2、打开文件-mode
  • 下面用两张表来概述,读者在前期可以多对照这张表来学习

img

img

# 打开方式,默认是t(text文本)
# 磁盘(bytes) -> text(encoding)
# 使用bytes打开文件时,不需要指定编码方式
# ValueError: binary mode doesn't take an encoding argument
fp = open('today.txt', 'rb')
print(fp.readlines())
fp.close()

# w 每次都会清空文件内容
fp = open('today1.txt', 'w+')
print(fp.readlines())
fp.close()

# a (文件不存在会创建新文件 ) => 追加
fp = open('today2.txt', 'a+')
# print(fp.readlines())
fp.write("你好呀")
fp.close()
3、打开文件-文件编码

encoding: 文件的编码方式( str )

encoding的默认值:None, 不同的平台打开的方式不一样

• encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent。

# 打开文件(windows上打开文件, 默认编码gbk, linux utf-8)
# 注意:使用正确的打开方式,否则可能乱码或报错
fp = open('today.txt', encoding="gbk")
print(fp.readlines())
fp.close()
4、关闭文件-close
  • 为什么要关闭文件

1、将写缓存同步到磁盘;

2、linux系统中每个进程打开文件的个数是有限的;

3、如果打开文件数到了系统限制,在打开文件就会失败

  • 关闭文件-用with语句打开,完成后会自动关闭文件

三、读取文件

img

# 从开头移动 -> 位移量为正 -> 使用r模式打开文件
with open('today1.txt', 'r+') as fp1:
    print("当前位置:", fp1.tell())
    fp1.readlines()
    print("当前位置:", fp1.tell())
    fp1.seek(2, 0)
    print("当前位置:", fp1.tell())


# 从末尾移动 -> 位移量一般是负数 -> 使用b模式打开文件
fp1 = open('today2.txt', 'rb')
print("当前位置:",fp1.tell())
fp1.read()
fp1.seek(-1,2)
print("当前位置:",fp1.tell())

# 从当前位置移动 -> 位移量可正可负数 -> 使用b模式打开文件
fp1 = open('today2.txt', mode='rb')
print("当前位置:",fp1.tell())
fp1.read()
fp1.seek(-2, 1)
print("当前位置:",fp1.tell())

#当前位置: 0
#当前位置: 6
#当前位置: 2
#当前位置: 0
#当前位置: 15
#当前位置: 0
#当前位置: 14

练习

img

with open("example.txt",'r',encoding='gbk') as fp:
   #读取文件的前五行,前五百行只需要修改一个参数
   for i in range(5):
       print(fp.readline())
   #读取全文,每次都要把光标移到开始的位置
   fp.seek(0,0)
   print(fp.read())
   fp.seek(0,0)
   print(fp.readlines())
#读取文件的最后10个字节,2只能用b模式打开才可以
with open("example.txt",'rb') as fp:
   fp.seek(-10,2)
   print(fp.read())

四、写文件

  • 为什么不实时写入磁盘
    • 硬盘是慢设备,频率读写会增大磁盘压力,产生瓶颈
  • 什么时候会写入磁盘
    • f.flush() # 只在二进制模式下生效
    • f.close()
    • buffer设置(默认:io.DEFAULT_BUFFER_SIZE )
      • 0 => 实时写入 (binary mode)
      • 1 => 行缓存 ( text mode)
      • 其他数字n => 缓冲区大小n : 2*4096

五、文件对象的其他方法

img

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Pod️

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

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

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

打赏作者

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

抵扣说明:

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

余额充值