文件相关操作


#非文字类文件,用rb,上传下载都是二进制,utf-8,gbk形式rb
# f = open('f:\123.txt', mode="rb",)
# content = f.read()
# f.write("you ae".encode("utf-8"))
# print(content)
# print(f)
# f.close()

#bypes ---------> str R

# f = open("123", mode="r", encoding="utf-8")
#
# content = f.read()
#
# print(content,type(content))
# print(f,type(f))
# f.close()

#W----> no file-->build have file-->first delete all content and then write

# f = open("log", mode="w", encoding="utf-8")
# f.write("can you understand")
# f.close()
# WB

# f = open("log", mode="wb")
# f.write("123".encode("utf-8"))
# f.close()

#add --->append()
# f = open("log", mode="a", encoding="utf-8")
# f.write("you are so beautiful")
#
# f.close()
# ab---->btype
# f = open("log", mode="ab")
# f.write("i think you are ".encode("utf-8"))
# f.close()

#read and write
# f = open("log",mode="r+", encoding="utf-8")
# print(f.read())
# f.write("you are so lovely")
#
# f.close()
#r + b
# f = open("log",mode="r+b")
# print(f.read())
# f.write("you are so lovely".encode("utf-8"))
#
# f.close()
#write and read
# f = open("log",mode="r+", encoding="utf-8")
# f.write("aaazzzzzzzzzz")
# print(f.read())
# f.close()
#change the mouse position
# f.seek(0)#begin


# f = open("log", mode="r+", encoding="utf-8")
# f.seek(3)#按照字节找,,,一个中文三个字节
# print(f.tell())#告诉光标的位置

# content = f.read()
# print(content)
# f.close()

原文地址:https://www.cnblogs.com/jly1/p/9570290.html