zoukankan      html  css  js  c++  java
  • python基础第7天(day19)

    文件处理


    # f=open("test2")

    #f=open("test",encoding="utf8")
    #f=open("test3",mode="w",encoding="utf8")
    #f=open("test3",mode="w",encoding="utf8")
    #f=open("test5",mode="a",encoding="utf8")
    #f=open("test5",mode="x",encoding="utf8")#存在
    #f=open("test5",mode="rb")
    f=open("test5",mode="wb+")


    #--------------------读操作
    #data=f.read()

    # data=f.read(5) # 读指定个数的字符
    # data2=f.read(5) #从光标位置 读指定个数的字符

    # data=f.readline() #读某一行
    # data2=f.readline()

    # data=f.readlines() #返回的是一个列表结果,以 结尾


    #print(data)
    #print("data2",data2)
    # f.close()


    #练习

    # count=0

    # for line in f.readlines():#会把所有文件放到内存,不建议用
    # if count==3:
    # line="".join([line.strip(),"岳飞"])
    #
    # print(line.strip())
    # count+=1


    # for line in f: # 优化内存,建议用,每次拿一行
    # if count==3:
    # line="".join([line.strip(),"岳飞"])
    #
    # print(line.strip())
    # count+=1

    #--------------------写操作

    #print(f.read()) #不可读
    #
    # f.write("yyyy world2")
    # f.flush()
    # f.write("iiii world2")


    #--------------------flush操作


    # f.write("hellp test5")
    # f.flush()
    #
    # import time
    # time.sleep(100)

    #进度条:


    # import sys
    #
    # for i in range(100):
    #
    # #
    #
    # s=" %d%% %s"%(i,"#"*i)
    #
    # sys.stdout.write(s)
    # sys.stdout.flush()
    #
    # import time
    # time.sleep(0.5)


    #可读可写模式 r+ w+ a+

    # r+ 如果写的话是追 加写,读是一样读

    # f=open("test5",mode="r+",encoding="utf8")
    # print(f.read())
    #
    # f.write("where is xialv?")


    #w+ ,先复盖再读取,一定是先清空,再写

    #f=open("test5",mode="w+",encoding="utf8")
    # f=open("test5",mode="r+",encoding="utf8")
    #
    # print(f.read(3))
    #
    # #f.write("hello林海峰")
    # #f.seek(3,0) #将光标移到开始位置,不同于read()方法,它是按字节移动的
    # #f.seek(1,-2) #将光标移到开始位置,不同于read()方法,它是按字节移动的 欠着
    #print(f.read(3))
    #f.seek(1,0)

    #f.seek(-3,2) #该模式一定按字节操作

    #print("----")
    #print(f.read().decode("utf8"))
    #
    # print(f.read())
    #print(f.tell())#光标所在位置


    # f.write(b"hello")
    #
    # print(f.read())
    # f.seek(-1,2)
    # print(f.read())

    # f.close()


    # a+ 总是在最后位置添加,无论光标在哪,都是在最后写

    # f=open("test5","a+")
    #
    # f.seek(0)
    # print(f.read())
    #
    # f.seek(0)
    #
    # f.write("alex")


    #------------总结

    # r w a w:覆盖写 a:追加写

    # r+:读默认光标在开始位置, 写的追加写
    # w+:写覆盖写(上来就先复盖),想读取内容:seek调整
    # a+: 光标默认在文件最后位置,不管光标位置,一定是追加写;想读取内容:seek调整
    因为w+跟a+一定是光标在最后的,所以你要是想读的话, 一定要调整光标,所以这个光标是有用的

    #seek的应用,比如断点续传


    # f.seek(10000)
    # f.read()

    #--------- rb wb ab 二进制操作 b操作不需要encodeing这个操作,因为他直接就是用来传输什么的,直接放到内存中是字节

    # f=open("test5","rb")
    # print(f.read())

    # f=open("test6","wb")
    #
    # f.write("hello李杰".encode("utf8"))
    #
    # f.close()
    #
    # f=open("test6","ab")

    # with

    # f=open("rest6")
    # f.read()
    # f.close()

    # with open("test6") as f: # f=open("test6")
    # f.read()

  • 相关阅读:
    chr(9) chr(10) chr(13) chr(32)
    分割字符串
    日期提取函数EXTRACT
    数据泵在本地导出数据到远程数据库中
    CEIL与FLOOR
    GROUPING SETS与GROUP_ID
    LISTAGG
    AVG
    COUNT
    Scala 泛型类型和方法
  • 原文地址:https://www.cnblogs.com/wanchenxi/p/7259978.html
Copyright © 2011-2022 走看看