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
标签:errors,encoding,Python,读写,Py,文本文件,print,txt,DIP From: https://www.cnblogs.com/lnsylt/p/17754513.html