高级农民工(H.F.H)
我思,故我在!(僕なら、僕ができる!)

导航

 

start

#读1
try:
    f = open("C:/DIP-PC/lt/workspace/Py_Wk/source/temp.txt","r",encoding="UTF-8",errors="ignore")    
    #全读(无参)
    # print(f.read())

    #按行读
    for line in f.readlines():
        print("read1",line.strip())
finally:
    if f:
        f.close()

#读2
with open("C:/DIP-PC/lt/workspace/Py_Wk/source/temp.txt","r",encoding="UTF-8",errors="ignore") as f:
    #全读(无参)
    # print(f.read())

    #按行读
    while True:
        s = f.readline()
        if s=="":
            break;
        print("read2",s.strip())

#读3
try:
    f = open("C:/DIP-PC/lt/workspace/Py_Wk/source/temp.txt","r",encoding="UTF-8",errors="ignore")    
    position =f.tell()
    print(f"当前文件指针位置:{position}")

    #部分读(有参,10字节)
    content =f.read(10)
    print(f"内容:{content}")

    position =f.tell()
    print(f"当前文件指针位置:{position}")
    
finally:
    if f:
        f.close()

#写(w),追加(a)
# with open("C:/DIP-PC/lt/workspace/Py_Wk/source/temp.txt","w",encoding="UTF-8",errors="ignore") as f:
#     f.write("welcome.....")

另:

 

 

 

end

posted on 2023-10-10 13:59  农民工024  阅读(10)  评论(0编辑  收藏  举报