zoukankan      html  css  js  c++  java
  • 文件操作

    一.简介

    fp = open(文件名,模式,字符编码)

    返回文件io对象 => fp(文件句柄)

    i => input 输入

    o => output 输出

    文件内容可以写入1.字符串 2.字节流

    二.文件的操作

    1.1文件的写入操作

    # 1.打开文件
    fp = open("lianxi.txt",mode="w",encoding="utf-8") # 打开冰箱门
    # 2.写入文件
    fp.write("把大象塞进去") # 把大象塞进去
    # 3.关闭文件
    fp.close() # 关上冰箱门

    1.2文件的读取操作

    # 1.打开文件
    fp = open("lianxi.txt",mode="r",encoding="utf-8")
    # 2.读取文件
    res = fp.read()
    # 3.关闭文件
    fp.close()
    print(res)

    1.3字节流

    什么是字节流:

    """
    字节流语法格式(前面加b) : b"123"
    二进制字节流的应用场景: 在数据传输或者在数据存储时,使用的一种数据格式;
    多个字符放在一起叫做字符串,多个字节放在一起叫做字节流
    """


    # 将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式)
    #encode() 编码 将字符串转化为字节流(Bytes流)
    #decode() 解码 将Bytes流转化为字符串

    # 把中文变成字节流
    # encode 编码
    strvar = "我爱你".encode("utf-8")
    print(strvar)
    
    # decode 解码
    res = strvar.decode("utf-8")
    print(res)
    
    # 三个字节表达一个中文字符
    s_bytes = b'xe7x88xb1'
    res = s_bytes.decode("utf-8")
    print(res)

    1.1写入字节流

    """mode = wb 代表写入的是字节流 , 不要指定任何编码集 """
    # 1.打开文件
    fp = open("lianxi2",mode="wb")
    strvar = "爱上一匹野马,家里没有草原"
    # 2.写入字节流
    fp.write(strvar.encode())
    # 3.关闭文件
    fp.close()

    1.2读取字节流

    """mode = rb 代表读取的是字节流 , 不要指定任何编码集 """
    # 1.打开文件
    fp = open("lianxi2",mode="rb")
    # 2.读取字节流
    res = fp.read()
    # 3.关闭文件
    fp.close()
    print(res)
    print(res.decode())

    1.4复制图片和视频

    图片:

    # ### 复制图片
    # 打开原文件,读取其中的字节流
    fp = open("集合.png",mode="rb")
    res = fp.read()
    fp.close()
    print(res)
    
    # 把这些字节流写入到另外文件中
    fp = open("集合2.gif",mode="wb")
    fp.write(res)
    fp.close()

    视频:

    # ### 复制视频
    fp = open("ceshimv.mp4",mode="rb")
    res = fp.read()
    fp.close()
    print(res)
    
    fp = open("ceshimv2.mp4",mode="wb")
    fp.write(res)
    fp.close()

    三 文件的扩展模式

    """
    # (utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节)
    #read() 功能: 读取字符的个数(里面的参数代表字符个数)
    #seek() 功能: 调整指针的位置(里面的参数代表字节个数)

    # seek(0) 把光标移动到文件开头
    # seek(0,2) 把光标移动到文件末尾

    #tell() 功能: 当前光标左侧所有的字节数(返回字节数)
    """

    1.1 r+ 先读后写

    fp = open("lianxi.txt",mode="r+",encoding="utf-8")
    # 先读
    res = fp.read()
    # 后写
    fp.write("456")
    # 在读
    fp.seek(0)
    res = fp.read()
    print(res)
    fp.close()

    1.2 r+ 先写后读

    fp = open("lianxi.txt",mode="r+",encoding="utf-8")
    # 先写
    fp.seek(0,2)
    fp.write("789")
    # 后读
    fp.seek(0)
    res = fp.read()
    print(res)
    fp.close()

    1.3 w+ 可读可写

    fp = open("lianxi2.txt",mode="w+",encoding="utf-8")
    # 先写
    fp.write("123")
    # 后读
    fp.seek(0)
    print(fp.read())
    fp.close()

    1.4 a+ 可写可读

    fp = open("lianxi3.txt",mode="a+",encoding="utf-8")
    fp.write('abc')
    fp.seek(0)
    print(fp.read())
    fp.close()

    四 文件的相关方法

    1.1刷新缓冲区

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

    1.2文件的相关函数

    1.1 readable()     功能: 判断文件对象是否可读

    fp = open("lianxi.txt",mode="r",encoding="utf-8")
    res = fp.readable()
    print(res)

    1.2 writable()  功能: 判断文件对象是否可读

    fp = open("lianxi.txt",mode="r",encoding="utf-8")
    res = fp.writable()
    print(res)

    1.3 readline()     功能: 读取一行文件内容

    """
    with open("lianxi.txt",mode="r",encoding="utf-8") as fp:
        res = fp.readline()
        print(res)
        res = fp.readline()
        print(res)
        res = fp.readline()
        print(res)
    """
    # (1).先读取一行,如果内容不是空,打印这行数据,在读取下一行进行判断
    with open("lianxi.txt",mode="r",encoding="utf-8") as fp:
        res = fp.readline()
        while res:
            print(res)
            res = fp.readline()
    
    # (2).readline(2) 2个字符个数
    """
    如果读取个数 > 当前行总个数 : 按照当前行读取
    如果读取个数 < 当前行总个数 : 按照个数读取
     """
    View Code

    1.4 readlines    功能: 将文件中的内容按照换行读取到列表当中

    with open("唐诗三百首",mode="r+",encoding="utf-8") as fp:
        lst = fp.readlines()
        print(lst)
    lst_new = []
    for i in lst:
        print(i)
        lst_new.append(i.strip())
    print(lst_new)

    1.5 writelines()   功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据

    lst_new = ['春苗不洗脚', '处处蚊子咬', '夜来大狗熊', '狼狗也来了','一个也跑不了']
    with open("宋词三百首",mode="w+",encoding="utf-8") as fp:
        fp.writelines(lst_new)
    
    # 在内容列表中插入一句话,叫做'狼狗也来了'放在跑不了的前面,以换行的形式插入到文件中
    lst_new = ['春苗不洗脚', '处处蚊子咬', '夜来大狗熊','一个也跑不了']
    lst_new.insert(-1,"狼狗也来了")
    print(lst_new)
    lst_new2 = []
    with open("王世名言",mode="w+",encoding="utf-8") as fp:
        for i in lst_new:
            lst_new2.append( i + '
    ')
        print(lst_new2)
        # lst = [1,2,3]
        # fp.writelines(lst) error 要求是字符串
        fp.writelines(lst_new2)
    View Code

    1.6 truncate()       功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)

    with open("lianxi4.txt",mode="r+",encoding="utf-8") as fp:
        fp.truncate(9)
  • 相关阅读:
    小程序mpvue 关闭eslint
    微信开发者工具更新后报错navigationBarBackgroundColor不是hexColor?
    微信小程序图片裁剪插件image-cropper
    A小程序跳转到B小程序,B小程序如何获取参数?
    EditText取消焦点
    RxJava + Retrofit
    TextView显示内容不全
    Android之StrictMode
    CoordinatorLayout实现的效果(标题栏效果)
    Rxbus的使用
  • 原文地址:https://www.cnblogs.com/whc6/p/14070486.html
Copyright © 2011-2022 走看看