• Python中文件操作



    本文大概2800字,阅读大概需要8分钟。

    一、文件操作

    opne(path, mode) 常用参数(path路径,mode模式)。
    open函数返回打开文件的流,可以在这个流上进行相对应的操作。mode模式默认为r
    模式:

    r: read 读(纯文本)、w: write写 (纯文本)
    rb: read binary 读(二进制)、wb: write binary写 (二进制)

    1.1、文件读取

    读取文件:

    read() 读取全部
    readline() 每次读取一行内容
    readlines() 读取所有的行保存到列表中
    readable() 判断是否可读

    例:读取全部

    stream = open(r'/Users/justin/Desktop/python_dic.txt')
    
    container = stream.read()
    print(container)
    
    • 1
    • 2
    • 3
    • 4

    效果:
    在这里插入图片描述


    也可以读取图片,不过图片是二进制的,必须用rb才能读取,否则会保存,例如:

    stream = open(r'/Users/justin/Desktop/python.jpg', mode="rb")
    
    container = stream.read()
    print(container)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    因为终端显示不了图片,所以可以看到显示一堆二进制。


    1.2、文件追加和写操作

    写操作:
    模式只要是w模式的话,就是写模式,w的话每次写的话,都会把里面的内容清空重新写。
    写文件:

    stream = open(r'/Users/justin/Desktop/python_dic.txt', 'w')
    w = 'file fourth line'
    stream.write(w)
    stream.close() #  释放资源
    
    • 1
    • 2
    • 3
    • 4

    效果:
    在这里插入图片描述
    可以看到原先里面的内容没了。


    追加操作:
    模式为a模式,就是追加操作:

    stream = open(r'/Users/justin/Desktop/python_dic.txt', 'a')
    w = '\nfile fifth line'
    stream.write(w)
    stream.close()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述


    1.3、文件复制

    with结合open使用,可以帮助我们自动释放资源。

    现在我的桌面有一张python.jpg,我要在下载文件夹复制一张一样的图片,例:

    with open(r'/Users/justin/Desktop/python.jpg', 'rb') as stream:
        container = stream.read()
        with open(r'/Users/justin/Downloads/python.jpg', 'wb') as wstream:
            wstream.write(container)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    效果:
    在这里插入图片描述
    可以看到复制过来了。


    二、os模块

    如果我们需要复制文件夹的话就必须引用os模块进行处理。

    2.1、绝对路径

    os.path.isabs(路径),判断是否为绝对路径,返回布尔类型。

    import os
    
    print(os.path.isabs('/Users/justin/Desktop/python.jpg'))
    
    • 1
    • 2
    • 3

    在这里插入图片描述


    获取当前文件的绝对路径:
    os.path.abspath(__file__) 或者os.getcwd()

    import os
    
    path = os.path.abspath(__file__)
    path1 = os.getcwd()
    
    print(path)
    print(path1)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    效果:
    在这里插入图片描述


    2.2、递归复制文件夹

    创建一个文件夹:os.mkdir(r'/Users/justin/Desktop/a1')

    删除一个文件夹(只能删除不是空的文件夹):os.rmdir(r'/Users/justin/Desktop/a1')

    遍历一个文件夹里的文件或者文件夹:os.listdir()

    import os
    
    
    # 封装成函数
    def copy(src, target):
        if os.path.exists(target):
            pass
        else:
            os.mkdir(target)
        # 获取文件夹里的内容
        filelist = os.listdir(src)
        # 遍历列表
        for file in filelist:
            # 拼接路径
            path = os.path.join(src, file)
            # 判断是文件还是文件夹
            if os.path.isdir(path):
                # 如果是文件夹,递归调用copy
                # 拼接并且创建新的文件夹
                target_path1 = os.path.join(target, file)
                os.mkdir(target_path1)
                copy(path, target_path1)
            else:
                # 不是文件夹直接进行复制
                with open(path, 'rb') as rstream:
                    container = rstream.read()
                    path1 = os.path.join(target, file)
                    with open(path1, 'wb') as wstream:
                        wstream.write(container)
        else:
            print('复制完毕')
    
    
    # 调用函数
    copy('/Users/justin/Desktop/a1', '/Users/justin/Desktop/a2')
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    效果:
    在这里插入图片描述
    可以看到这里我们就实现了文件夹的递归复制。

    如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持

  • 相关阅读:
    Prompt-Tuning源码分析
    好用的截图软件Snipaste2.7.3
    使用搭载骁龙 8 Gen 3 的安卓手机运行 AI 大模型
    sqlserver存储过程报错:当前事务无法提交,而且无法支持写入日志文件的操作。请回滚该事务。
    SDL播放pcm无声音的原因
    fisco Java-sdk 快速入门案例
    Django--29用户权限设计
    MyBatis-Plus中的Mapper用法
    注意力机制
    算法通关村第二关终于学会链表反转了
  • 原文地址:https://blog.csdn.net/weixin_44103733/article/details/125921949