zoukankan      html  css  js  c++  java
  • 文件操作(十二)——open,read,close,write,seek,truncate

    open函数

    #!/usr/bin/env python
    #-*- coding:utf8 -*-
    
    f = open('xxx','r',encoding='utf-8')
    data = f.read()
    print(data)
    f.close()
    
    # readlines() 读多行,以列表的形式返回
    f = open('xxx','r',encoding='utf-8')
    data = f.readlines()    # ['111111
    ', '2222
    ', '33333
    ', '44444
    ', '555555']
    print(data)
    f.close()
    des = open('xxx_new', 'w', encoding='utf-8')
    des.write(data[0]) # 新文件的内容为  : 111111
    des.close()
    # writelines 传入一个列表
    des = open('xxx_new', 'w', encoding='utf-8')
    des.writelines(data)
    des.close()
    # 追加模式‘a'
    
    f = open('xxx','a+',encoding='utf-8')
    f.write('666666
    ')
    data = f.readlines()
    print(data)
    f.close()
    
    # readline() 读一行
    f = open('xxx','r',encoding='utf-8')
    data = f.readline()
    print(data)
    f.close()
    
    '''
    引用另外一个文件的函数
    '''
    import test_import
    test_import.test()

    文件模式:

    rb模式,wb模式

    f = open('xxx','rb') # b 模式不能指定编码
    data = f.read()
    print(data)
    f.close()
    '''
    hello
    2222
    33333
    你好世界
    b'hello
    2222
    33333
    xe4xbdxa0xe5xa5xbdxe4xb8x96xe7x95x8c'
    '''
    f = open('xxx','rb') # b 模式不能指定编码
    data = f.read() # 都出来的内容就是二进制的
    print(data.decode('utf-8'))     # 把读出来的二进制解码,将显示为正常的字符串
    f.close()
    '''
    hello
    2222
    33333
    你好世界
    '''
    
    f = open('test_1','wb') # b 模式不能指定编码
    str1 = '1111
    '
    str2 = '2222
    '
    str11 = bytes(str1,encoding='utf-8')
    f.write(str11)
    f.write(str2.encode('utf-8'))
    f.close()

    tell(),seeek(),truncate()

    # 除了read() 是以字符为单位读取,其他的是以字节为单位读取
    
    f = open('test_1','r',encoding='utf_8',newline='') # newline 还原真的换行符,windows:
    ,linux:
    
    data = f.readline()
    print(data.rstrip('
    '))    #截掉字符串的空格或指定字符(	,
    也可以)。
    print(f.tell()) #   文件光标的位置
    f.seek(0)   # 移动光标的位置
    print(f.tell())
    
    data = f.readlines()
    print(data)
    f.close()
    
    f = open('test_1','r+',encoding='utf_8')
    f.truncate(5)   # 截断,必须要有写的权限, 保存截断参数之前的内容
    f.close()

    seek(), 有三种模式,whence对应取:0,1,2

    0:表示从文件开头位置,也是默认模式

    1:相对位置,相对位置要以‘b‘形式打开文件

    2:倒着seek

    def seek(self, offset: int, whence: int = 0) -> int:
    f = open('test_1','rb')
    print(f.tell()) # 0
    data = f.readlines()
    print(f.tell()) # 31
    f.seek(-5,2) # 2 从结尾读取5个字节
    print(f.tell()) # 26
    data = f.read()
    print(data)


    利用seek读取文件最后一行的内容

    f = open('test_1','rb')
    for i in f:
        pos = -2
        while True:
            f.seek(pos, 2)  # 每次从文件末尾偏移pos个字节开始读
            data = f.readlines()
            if len(data) > 1:
                print("文件的最后一行是 %s" %(data[-1]).decode('utf-8'))
                break
            pos *= 2
  • 相关阅读:
    SQL语句(十二)分组查询
    SQL语句(十一)函数查询
    Markdown公式
    处理机调度(一)——处理机调度概念
    进程控制(一)——进程创建
    线程(二)—— 用户线程和内核线程
    进程(三)—— 进程的状态
    进程(二)—— 进程控制块
    进程(一)—— 进程概念
    Java迭代器用法
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10402744.html
Copyright © 2011-2022 走看看