zoukankan      html  css  js  c++  java
  • python第四章文件操作

    第四章 文件操作

    4.1 文件基本操作

    obj = open('路径',mode='模式',encoding='编码')   #  打开文件
    obj.write()    #  把内容写入文件
    obj.read()     #  读取文件内容
    obj.close()    #  关闭文件(保存文件,把内存上的数据写入到文件上-->硬盘上,01010101存储的)
    
    # 3步:1.打开文件.  2. 操作文件   3. 关闭文件
    

    4.2打开文件

    4.2.1语句

    file = open('文件路径',mode = 'r',encoding = 'utf-8')
    # 文件路径:     D:文件夹名字文件夹
    # encoding = 'utf-8',以utf-8编码方式打开.
    #########或者用另外一种语句:
    with open(''文件路径',mode = 'r',encoding = 'utf-8') as file:
    #或者 同时打开2个文件.下面的代码需要缩进,且不需要.close()语句.
    with open(''文件路径',mode = 'r',encoding = 'utf-8'') as file ,open(''文件路径',mode = 'r',encoding = 'utf-8'') as file2 :
    	v1 = file.read()
        v2 = file2.read()
    

    4.2.2打开文件的模式(mode)

    • r / w / a 只读只写字符串

    • r+ / w+ / a+ 可读可写字符串

      1. 读取:r,只能读文件,默认的模式.文件不存在就报错.
      2. 写入:w,只能写文件,文件不存在则创建,文件存在则清空内容在写入. .
      3. 追加:a,只能追加,文件不存在则创建,文件存在则不会覆盖,写内容会以追加的方式写 (写日志文件的时候常用 ).
      4. 可读可写:r+
        • 读:默认从0的光标开始读,也可以通过 seek 函数调整光标的为位置
        • 写:从光标所在的位置开始写,也可以通过 seek 调整光标的位置
      5. 可读可写:w+
        • 读:默认光标永远在写入的最后,也可以通过 seek 函数调整光标的位置
        • 写:先清空
      6. 可读可写:a+
        • 读:默认光标在最后,也可以通过 seek 函数 调整光标的位置。然后再去读取
        • 写:永远写到最后
    • rb / wb / ab 只读只写二进制

      file = open('文件路径',mode = 'wb')  # rb/wb/ab模式,不需要encoding = 'utf-8'
        #注意,如果是/rb/ab/wb模式,写入和读取的必须是二进制,即010100010 010110101 0101000,否则报错.写入数据是
      data = '你好,世界'.encode('utf-8')    #将字符串转化为utf-8编码方式的二进制数据.
      file.write(data)
      file.close()  
      print(v)
      ########
      
    • r+b / w+b / a+b 可读可写二进制 , r+b/w+b/a+b模式同上.

    4.3 文件操作

    • read() , 全部读到内存

    • read(1)

      • 1表示一个字符

        obj = open('a.txt',mode='r',encoding='utf-8')
        data = obj.read(1) # 1个字符
        obj.close()
        print(data)
        
      • 1表示一个字节

        obj = open('a.txt',mode='rb')
        data = obj.read(3) # 1个字节
        obj.close()
        
    • readlins()

      date_list = file.readlines() #  读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素,但读取大文件会比较占内存.
      
    • 大文件读取(50g的文件,内存没有这么大)

      file = open('文件路径',mode = 'r',encoding = 'utf-8')
      ###  2.如果以后读取一个特别大的文件,可以一行一行读取
      for line in file:
          line = line.strip()     #去除换行符(默认去除换行符
      ),也可以填其他.
          print(line)             #一行一行读取,
      
    • write(字符串,utf-8)

      obj = open('a.txt',mode='w',encoding='utf-8')
      obj.write('中午你')
      obj.close()
      
    • write(二进制)

      obj = open('a.txt',mode='wb')
      
      # obj.write('中午你'.encode('utf-8'))
      v = '中午你'.encode('utf-8')
      obj.write(v)
      
      obj.close()
      
    • seek函数:(光标字节位置) 用于移动文件读取光标到指定位置,无论模式是否带b,都是按照字节进行处理。

      obj = open('a.txt',mode='r',encoding='utf-8')
      obj.seek(3) # 跳转到指定3字节位置
      data = obj.read()
      obj.close()
      
      print(data)
      ##################################################
      obj = open('a.txt',mode='rb')
      obj.seek(3) # 跳转到指定字节位置
      data = obj.read()
      obj.close()
      
      print(data)
      ######################################
      fileObject.seek(offset[, whence])
      #  offset -- 开始的偏移量,也就是代表需要移动偏移的字节数.以字节为单位.
      #  whence:可选,默认值为 0。给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。如seek(0,0)
      
    • tell(), 获取光标当前所在的字节位置

      obj = open('a.txt',mode='rb')
      # obj.seek(3) # 跳转到指定字节位置
      obj.read()
      data = obj.tell()
      print(data)
      obj.close()
      
    • flush,强制将内存中的数据写入到硬盘

      v = open('a.txt',mode='a',encoding='utf-8')
      while True:
          val = input('请输入:')
          v.write(val)
          v.flush()      # 避免内存泄漏
      
      v.close()
      

    4.4 关闭文件

    一般方法

    v = open('a.txt',mode='a',encoding='utf-8')
    # 文件操作
    v.close()
    

    避免忘记输入.close()的方法.

    with open('a.txt',mode='a',encoding='utf-8') as v:
        v.write('这是另一种文件打开方法')
    	# 缩进中的代码执行完毕后,自动关闭文件
    

    4.5 文件内容的修改

    with open('a.txt',mode='r',encoding='utf-8') as f1:
        data = f1.read()
    new_data = data.replace('飞洒','666')
    
    with open('a.txt',mode='w',encoding='utf-8') as f1:
        data = f1.write(new_data)
    
    • 大文件修改
    f1 = open('a.txt',mode='r',encoding='utf-8')
    f2 = open('b.txt',mode='w',encoding='utf-8')
    
    for line in f1:
        new_line = line.replace('阿斯','死啊')
        f2.write(new_line)
    f1.close()
    f2.close()
    
    with open('a.txt',mode='r',encoding='utf-8') as f1, open('c.txt',mode='w',encoding='utf-8') as f2:
        for line in f1:
            new_line = line.replace('阿斯', '死啊')
            f2.write(new_line)
    

    img新人上路,请多多批评指正img

  • 相关阅读:
    随机森林算法参数调优
    BAYES和朴素BAYES
    阿里云 金融接口 token PHP
    PHP mysql 按时间分组 表格table 跨度 rowspan
    MySql按周,按月,按日分组统计数据
    PHP 获取今日、昨日、本周、上周、本月的等等常用的起始时间戳和结束时间戳的时间处理类
    thinkphp5 tp5 会话控制 session 登录 退出 检查检验登录 判断是否应该跳转到上次url
    微信 模板消息
    php 腾讯 地图 api 计算 坐标 两点 距离 微信 网页 WebService API
    php添加http头禁止浏览器缓存
  • 原文地址:https://www.cnblogs.com/deng1821333144/p/10836893.html
Copyright © 2011-2022 走看看