• python_文件操作


    """只读:r"""
    # f = open('G:丁杰.txt')
    # content = f.read()
    # print(content)
    # f.close()
    
    # f = open('丁杰.txt',mode='r',encoding='utf-8')
    # content = f.read()
    # print(content)
    # f.close()
    
    """
        读:rb  bytes类型
    """
    # f = open('丁杰.txt',mode='rb')
    # content = f.read()
    # print(content)
    # f.close()
    
    """
        r+: 读写
    """
    # f = open('log.txt',mode='r+',encoding='utf-8')
    # print(f.read())
    # f.write('v你就放心女客人女')
    # f.close()
    
    # f = open('log.txt',mode='r+',encoding='utf-8')
    # f.write('111')
    # print(f.read())
    # f.close()
    
    """
        r+b: 读写,bytes类型
    """
    # f = open('log.txt',mode='r+b')
    # print(f.read())
    # f.write('大猛,小孟'.encode('utf-8'))
    # f.close()
    
    """******************************"""
    
    """ 只读:w
        没有此文件就会创建文件
        有文件先将原文件的内容全部清除,再写
    """
    # f = open('log.txt',mode='w',encoding='utf-8')
    # f.write('我是你爸爸。')
    # f.close()
    
    # f = open('log.txt',mode='w',encoding='utf-8')
    # f.write('内存空间的够狠')
    # f.close()
    
    """
        wb :bytes类型写入
    """
    # f = open('log.txt',mode='wb')
    # f.write('内存空间的够狠'.encode('utf-8'))
    # f.close()
    
    """
        w+: 写读
    """
    # f = open('log.txt',mode='w+',encoding='utf-8')
    # f.write('aaa')
    # f.seek(0)        # 调节光标  seek
    # print(f.read())
    # f.close()
    
    
    """
        a:追加
    """
    # f = open('log.txt',mode='a',encoding='utf-8')
    # f.write('这个段文字是添加的')
    # f.close()
    
    """
        a+:写读
    """
    # f = open('log.txt',mode='a+',encoding='utf-8')
    # f.write('家琪')
    # f.seek(0)
    # print(f.read())
    # f.close()
    
    """
        ab: bytes类型
    """
    # f = open('log.txt',mode='ab')
    # f.write('的会计师v'.encode('utf-8'))
    # f.close()
    
    # 功能详解
    # f = open('log.txt',mode='r+',encoding='utf-8')
    # # content = f.read(3)   # read 读出来的都是字符
    # f.seek(3)               # seek 是按字节定光标的位置
    #
    # # f.tell()
    # print(f.tell())        #tell  获取光标位置
    # # content = f.read()
    # # print(content)
    # f.close()
    
    # f = open('log.txt',mode='r+',encoding='utf-8')
    # f.write('111')
    # count = f.tell()
    # print(count)
    # f.seek(count-9)
    # print(f.read())
    # f.tell()
    # f.readable    # 是否可读
    # line = f.readline()    # 一行一行的读 最小单位字符
    # lines = f.readlines()    # 每一行当成列表中的一个元素  ['附近的人,二哥,春哥,贤语哥
    ', 'bhjbjnm在彼此交换']
    # l1 = f.truncate(2)
    # for line in f:
    #     print(line)
    # f.close()
    
    # with open('log.txt',mode='r+',encoding='utf-8') as f,
    #         open('log.txt',mode='r+',encoding='utf-8') as f1:
    #     # f.read()
    #     print(f.read())
    
    """
        文件修改
    """
    with open('丁杰.txt',encoding='utf-8') as f ,open('丁杰.bak',mode='w',encoding='utf-8') as f1:
        for line in f:
            if '' in line:
                line = line.replace('','')
            f1.write(line)
    import os
    os.remove('丁杰.txt')     #删文件
    os.rename('丁杰.bak','丁杰.txt')  #重命名文件
  • 相关阅读:
    UVa 820 因特网带宽(最大流)
    UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)
    UVa 821 网页跳跃(Floyd)
    UVa 11624 大火蔓延的迷宫
    UVa 10881 蚂蚁
    UVa 11300 分金币
    UVa 11729 突击战
    《额尔古纳河右岸》读书笔记
    HDU 1083 Courses(二分图匹配模板)
    UVa 10618 跳舞机
  • 原文地址:https://www.cnblogs.com/jsit-dj-it/p/11256143.html
Copyright © 2020-2023  润新知