python基础知识(五)文件

(五)文件

目录

(五)文件

5.1文件的读取

5.2文件的写入

5.3文件的追加

5.4文件的修改

5.5其他操作


5.1文件的读取

  a.打开文件

#打开指定位置文件,绝对路径
f = open('d:\测试文件.txt',mode='r',encoding='utf-8')
content = f.read()
print(content)
f.close()

#在pycharm中打开文件,相对路径
f = open('测试文件.txt',mode='r',encoding='utf-8')
content = f.read()
print(content)
f.close()

   b.只读:r,读写:r+,以二进制格式打开一个文件用于只读:rb

r 只读
f = open('测试文件.txt',mode='r',encoding='utf-8')
content = f.read()
print(content)
f.close()

#先读后写,一开始光标在最前,读完之后在最后+qwe
f = open('测试文件1.txt',mode='r+',encoding='utf-8')
print(f.read())
f.write('qwe')
f.close()

#先写后读,光标一开始在最前,所以写入的内容会覆盖原内容,让背后把剩余内容读出来
f = open('测试文件1.txt',mode='r+',encoding='utf-8')
f.write('qwe')
print(f.read())
f.close()
f = open('测试文件.txt',mode='rb') #以bytes类型打开
content = f.read()
print(content)
f.close()  #b'内容'

   readline()与 readlines()

     readline():只读取一行

     readlines():从文件中读取每一行,并将其存储再一个列表中

#文件内容:123
          456
          789
with open('测试文件.txt',encoding='utf-8') as f:
    print(f.readline()) #123

with open('测试文件.txt', encoding='utf-8') as f:
   for i in f.readlines(): #f.readlines = ['123','456','789']打印要取\n
       print(i.strip())
'''
123
456
789
'''

5.2文件的写入

   只写:w,写读:w+,以二进制格式打开一个文件用于只写:wb

             先清空再写

#对于w:没有此文件就会创建文件
f = open('测试文件1.txt',mode='w',encoding='utf-8')
f.write('随便写点')
f.close()
#先将源文件的内容全部清除,再写。
f = open('测试文件1.txt',mode='w',encoding='utf-8')
f.write('随便打点')
f.close()

#以byte类型写入
f = open('测试文件1.txt',mode='wb')
f.write('随便打点',encode=('utf-8'))
f.close()
清空再写
f = open('测试文件1.txt',mode='w+',encoding='utf-8')
f.write('aaa')
print(f.read()) #读不到任何东西,光标已经移动到最后
f.close()

清空再写
f = open('测试文件1.txt',mode='w+',encoding='utf-8')
f.write('aaa')
f.seek(0) #把光标移到最前,读取aaa
print(f.read())
f.close()

5.3文件的追加

   只追加:a,追加读:a+,以二进制格式打开一个文件用于只追加:ab

#尾部追加哈哈
f = open('测试文件1.txt',mode='a',encoding='utf-8')
f.write('哈哈')
f.close()
f = open('测试文件1.txt',mode='a+',encoding='utf-8')
f.write('佳琪')
f.seek(0) #移动光标
print(f.read())
f.close()

5.4文件的修改

  文件不修改,只能覆盖 

#假装修改
f = open('测试文件.txt','w',encoding='utf-8')
f.write('未被修改')
f.close()
with open('测试文件.txt',encoding='utf-8') as f1,open('测试文件.bak','w',encoding='utf-8') as f2:
    for i in f1:
        if '未被修改' in i:
            i = i.replace('未被修改','已修改')
        f2.write(i)
import os
os.remove('测试文件.txt')  #删除文件
os.rename('测试文件.bak','测试文件') #改名

5.5其他操作

      a.read()

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

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

      b.seek()

             seek(n)光标移动到n位置,注意: 移动单位是byte字节,所有如果是utf-8的中文部分要是3的倍数

              移动到开头:seek(0)

              移动到结尾:seek(0,2) seek的第二个参数表示的是从哪个位置进行偏移,默认是0,表示开头,1表示当前位置,2表示结尾

      c. tell()

             使用tell()可以帮我们获取当前光标在什么位置

      d.另一种格式,不用close,可同时打开多个文件

with open('测试文件.txt',mode='r',encoding='utf-8') as f,open('测试文件.txt',mode='r',encoding='utf-8') as g:
    content = f.read()
    print(content)

    e.字符串----->encode------>bytes

       bytes----->decode------> 字符串

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值