zoukankan      html  css  js  c++  java
  • Python学习之路:文件操作之增删改查

    f = open("yesterday","r",encoding="utf-8")
    #print(f.read())
    #for i in range(5):
    #    print(f.readline()) #打印前5行
    
    #low loop
    '''
    for index,line in enumerate(f.readline()):
        if index == 9:
            print('---------我是分割线---------')
            continue
        print(line.strip())
    '''
    
    # high bige
    '''
    count =0
    for line in f:
        if count==9:
            print('---------我是分割线-----')
            count +=1
            continue
        print(line) #效率最高,一行一行的读
        count +=1
    '''
    f =open("yesterday2","w",encoding="utf-8")
    f.write("hello1
    ")
    f.write("hello2
    ")
    f.write("hello3
    ")
    f.flush()#实现数据从缓存刷到硬盘
    
    '''
    print(f.tell()) #查询光标位置
    print(f.readline())
    print(f.tell())#查询光标位置,按字符计数
    print(f.read(5))
    print(f.tell())
    f.seek(0)#光标回到某个字符位置
    print(f.readline())
    f.seek(10)
    print(f.readline())
    print(f.encoding)#打印文件编码
    print(f.fileno()) #读取文件编号
    #print(f.flush())
    print(dir(f.buffer))
    '''
    
    '''#进度条实现
    import sys,time
    
    for i in range(50):
        sys.stdout.write("#")
        sys.stdout.flush()
        time.sleep(0.1)
    '''
    
    f = open("yesterday","a+",encoding="utf-8")#追加读写
    #f.truncate(10) #截断
    f.seek(10)
    f.truncate(20)
    
    #可以打开,追加
    f = open("yesterday","r+",encoding="utf-8")#读写
    print(f.readline())
    print(f.readline())
    print(f.readline())
    print(f.tell())
    f.write('--------niu----------')
    
    #用处不大
    f = open("yesterday","w+",encoding="utf-8")#写读
    f.write('--------niu----------1
    ')
    f.write('--------niu----------2
    ')
    f.write('--------niu----------3
    ')
    print(f.tell())
    f.seek(10) #不能在中间插入,只能继续往后写,或者覆盖之前的
    print(f.tell())
    
    #使用场景:网络传输只能用二进制
    f = open("yesterday","rb")#以二进制格式读文件
    print(f.readline())
    print(f.readline())
    print(f.readline())
    
    f = open("yesterday","wb")#以二进制格式写文件
    f.write("hello binary
    ".encode())
    
    f = open("yesterday","ab")#以二进制格式追加文件
    
  • 相关阅读:
    微信支付-公众号支付,统一下单,调起微信支付,回调验证
    注册时 手机验证码的js验证和后台验证,调用阿里大于短信验证平台,手机号注册
    使用有模板的page类;thinkphp 分页使用的css
    thinkphp分页类使用,以及修改,添加了每页第一和最后的序号
    富文本编辑器Uediet使用
    Redis集群环境搭建
    Redis单机版安装
    Linux的shell编程
    Notepad++的列编辑功能
    SprngCloud微服务框架搭建(一)
  • 原文地址:https://www.cnblogs.com/xiaobai005/p/7773235.html
Copyright © 2011-2022 走看看