• Python 基础之文件操作与文件的相关函数


    一.文件操作

    fp =open("文件名",mode="采用的模式",encoding="使用什么编码集")
    fp 这个变量接受到open的返回值  是一个文件io对象 (又称文件句柄)
    i => input 输入
    o => output输出
    有了对象之后,就可以使用对象.属性 或者 对象.方法进行操作
    fp.write("字符串")
    fp.close() #关闭文件
    fp.read() #读取内容

    1.文件的写入和读取

    #(1)文件的写入
    #打开文件
    fp = open("test.txt",mode='w',encoding='utf-8')  #把冰箱门打开
    #写入文件
    fp.write("把大象放进去")
    #关闭文件
    fp.close()

    #创建了test.txt文本且文本内容如下:


    #(2)文件的读取
    #打开文件
    fp = open("test.txt",mode='r',encoding='utf-8')  #把冰箱门打开
    #读取文件
    res = fp.read()
    #关闭文件
    fp.close()
    print(res)
    #会输出:大象放进去

    2.将字符串和字节流(Bytes)类型进行转换

     (参数写成转化的字符编码格式)
        #encode() 编码  将字符串转化为字节流(Bytes)
        #decode() 解码  Bytes流转化为字符串
    #(1)
    strvar = "今天"
    bt = strvar.encode("utf-8")   #编码称成节流
    print(bt)
    res2 = bt.decode("utf-8")     #解码成字符串
    print(res2)

    #输出的结果为:



    #(2)wb  rb 模式 二进制字节流模式 (注意点:使用字节流模式的时候,不要指定encoding)
    #文件的写入

    #1:
    fp = open("test.txt",mode="wb")
    strvar = "好晴朗"
    res = strvar.encode("utf-8")
    fp.write(res)
    fp.close()
    #如果没有test.txt文本会创建一个并把”好晴朗”写入文件中.
    #文件的读取
    #2:

    fp = open("test.txt",mode="rb")
    #读取文件
    res = fp.read()
    fp.close()
    print(res)
    res2 = res.decode("utf-8")
    print(res2)

    #因为例1写入:好晴朗 输出的内容是分别是没有解码的”好晴朗”的二进制流和解码后的内容

    #(3)复制图片 (图片或视频之类的二进制字节流的内容,都可以使用b模式,比如wb,rb...)
    #打开文件
    fp = open("ceshi.png",mode="rb")
    #读取文件
    res = fp.read()
    #关闭文件
    fp.close()

    #打开文件
    fp = open("ceshi2.png",mode="wb")
    #写入文件
    fp.write(res)
    #关闭文件
    fp.close()

     

    3.扩展模式 + w+ r+ a+

    #(utf-8 编码格式下 默认一个中文三个字节 一个英文或字符 占用一个字节)
        #read() 功能:读取字符的个数(里面的参数代表字符个数)
        #seek() 功能:调整指针的位置(里面的参数代表字节个数)
        #tell() 功能:当前光标左侧所有字节数(返回字节数)

    把光标移动到文件行首
    fp.seek(0)
    把光标移动到文件末尾
    fp.seek(0,2)
    #(1)r+ 可读可写 (先读后写)
    fp = open("0506_3.txt",mode="r+",encoding="utf-8")
    res = fp.read()
    #print(res)  #先读出文件有什么内容
    fp.write("e") #从光标的位置写入内容
    #把光标移动到第0个字节上,那就是文件开头
    fp.seek(0)    #将文件的光标移动到开头
    res = fp.read() #从光标的位置开始读取文件内容
    print(res)
    fp.close()

    #(2)r+ 可读可写 (先写后读)
    fp = open("0506_3.txt",mode="r+",encoding="utf-8")
    #把光标移动到文件末尾
    fp.seek(0,2)
    fp.writelines("123")
    fp.seek(0)
    res = fp.read()
    print(res)
    fp.close()
    #(3)w+ 可读可写 (没有文件的话,会自动创建)
    fp = open("0506_4.txt",mode="w+",encoding="utf-8")
    fp.write("123456")
    fp.seek(0)
    res = fp.read()
    print(res)
    fp.close()
    #(4)a+ 可读可写 append (写入的时候,强制把光标移动到最后)
    fp = open("0506_4.txt",mode="a+",encoding="utf-8")
    fp.seek(0)
    fp.write("444444")
    fp.seek(0)
    res = fp.read()
    print(res)
    fp.close()
    #(5)tell read seek 注意 seek 括号里的字节数 read 括号里的字节数
    fp = open("0506_4.txt",mode="a+",encoding="utf-8")  #当前光标在内容的最后一个
    res = fp.tell()   #从光标左侧所有字节数,
    print(res)      #内容为:123456789  光标在9的后面,所以输出9
    fp.seek(5)      #将光标跳到第五个字节,
    res = fp.tell()    #所以光标在12345
    print(res)       #6 5和6的位置,所以输出5
    res = fp.read(2)  #读取2个字符(往后读两个字节)
    res = fp.tell()    #再次统计字节数(5+2)
    print(res)       #所以输出7
    fp.close()

    注意有中文的情况 如果移动错误 有可能读取不出来
    #:
    fp =open("0506_4.txt",mode="a+",encoding="utf-8")
    res = fp.tell()
    print(res)
    fp.seek(2)
    res = fp.tell()
    print(res)
    fp.read(2)
    res = fp.tell()
    print(res)
    #当前后面的字符是天 如果用seek移动一个字节
    fp.seek(5)
    res = fp.tell()
    print(res)
    res = fp.read()
    fp.close()

    #(6) with操作 语法 自动关闭文件 相当于帮你执行了fp.close
    # as 相当于起别名fp = open("0506_5.txt",mode="w",encoding="utf-8")
    #with 是一个代码块 不要落下冒号:
    with open("0506_5.txt",mode="w",encoding="utf-8") as fp:
        #往文件里面写的内容只能是字符串 或者 字节流
        fp.write("123")
    最后的内容:123

    二.文件的相关函数

     

    1.flush

    #刷新缓冲区 flush
        #当文件关闭的时候自动刷新缓冲区
        #当整个程序运行结束的时候自动刷新缓冲区
        #当缓冲区写满了 会自动刷新缓冲区
        #手动刷新缓冲区
    #1:
    fp = open("0506_6.txt",mode="w+",encoding="utf-8")
    fp.write("123445")
    #手动刷新缓冲区
    fp.flush()
    while True:
        pass
    fp.close()

    2.文件对象具有迭代性

    #readable()      功能: 判断文件对象是否可读
    #writable()      功能: 判断文件对象是否可写
    #1:
    fp =open("0506_6.txt",mode="r",encoding="utf-8")
    res1 = fp.readable()
    res2 = fp.writable()
    print(res1) #True
    print(res2) #False
    for i in fp:
        print(i)
    文件里面的内容为:
    123445
    231031
    32131y
    321y0

    3.文件的相关函数

    #(1)readline() 功能: 读取一行文件内容
    readline(字符数)
    如果给的参数大于当前行字符数,只获取当前行所有内容
    如果给的参数小于当前行字符数,按照实际给的参数进行字符的获取
    #:
    with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
        res = fp.readline(300)
        print(res)
        #0 0.0 0j False '' [] () set() {} None
        #循环打印文件里面每一行的内容
        #while res:
            #print(res)
            #res = fp.readline()
    #(2)readlines()  功能:将文件中的内容按照换行读取列表当中
    #:
    lst = []
    with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
        res = fp.readlines()

        for i in res:
            res2 = i.strip()
            lst.append(res2)
    print(lst)
    #(3)writelines() 功能: 将内容是字符串的可迭代性数据写入文件中
    # 参数:内容为字符串类型的可迭代数据
    #:
     with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
         #strvar = "今天天气好晴朗"
         lst = ["亲爱的 ","美女 ","请看我 ","一眼 "]
         fp.writelines(lst)
    效果如下:
    亲爱的
    美女
    请看我
    一眼
    # (4)truncate() 功能:把要截取的字符串提取出来,然后清空内容将提取的字符串重新下入文件中(字节)

    #:
    with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
         fp.seek(9)
         fp.truncate()  #截取6个字节的内容重新写入

    #效果是剩: 亲爱
    注意区分:
    read(字符)
    readline(字符)
    seek(字节)
    truncate(字节)
    tell返回的是字节

     

  • 相关阅读:
    MySql学习
    python学习笔记
    关于字符编码的理解
    session与cookie
    常用的表单元素
    Linq基础
    发送邮件。。
    进程与线程
    winform知识点集合
    winform基础与简单的窗体
  • 原文地址:https://www.cnblogs.com/hszstudypy/p/10850009.html
Copyright © 2020-2023  润新知