Hello everyone, I will answer the following questions for you, how to create a file in a file through python, how to create a folder and name it through python, let us take a look today!
# 1. File operation
# day1.txt
# 1. File path: E:\day1.txt
# 2. Encoding method: utf-8, gbk
# 3. Operation mode: read-only, write-only, append, read-write, write-read
# Save in any encoding method and open it in any encoding method
#absolute path
# f = open("E:\day1.txt",mode="r",encoding="gbk")
# count = f.read()
# print(count)
# f.close()
#relative path
# f = open("The weather is really nice today", mode="r", encoding="utf-8")
# count = f.read()
# print(count,type(count))
# f.close()
# Read-only: r, rb (non-text type files, upload, download, picture) bytes----str
# f = open("The weather is really nice today", mode="rb")
# count = f.read()
# print(count,type(count))
# f.close()
# write only: w
# For writing files, the file will be created without this file,
# f = open("The weather is good today", mode="w", encoding="utf-8")
# f.write("The weather is good today")
# f.close()
# Delete all the contents of the original file before writing
# f = open("The weather is good today", mode="w", encoding="utf-8")
# f.write("It's really good")
# f.close()
#wb The default encoding method of the file will be written in whatever encoding method
# f = open("The weather is good today", mode="wb")
# f.write("zhendeshi ".encode("utf-8"))
# f.close()
#Append a The default cursor is displayed on the last character, it can only be added but not
# f = open("The weather is good today", mode="a", encoding="utf-8")
# f.write("Amount")
# f.close()
#ab
# f = open("The weather is good today", mode="ab")
# f.write("Amount".encode("utf-8"))
# f.close()
#a+
# f = open("The weather is good today", mode="a+", encoding="utf-8")
# f.write("Amount")
# f.seek(0)
# print(f.read())
# f.close()
#Read and write r+ first read the original file, and then write
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# print( f.read())
# f.write("yiyi,erer")
# f.close()
#Read and write in bytes
# f = open("The weather is really nice today", mode="r+b")
# print( f.read())
# f.write("hahhahhahahhahahhahha".encode("utf-8"))
# f.close()
#Write and read w+ Clear the original file first, then write
# f = open("The weather is really nice today", mode="w+", encoding="utf-8")
# f.write("aaaaaaaaaaaaaaaaaaaaa")
# print(f.read())
# f.close()
#seek (adjust cursor)
# f = open("The weather is really nice today", mode="w+", encoding="utf-8")
# f.write("bbbbaaaaaaaaaaaaaaaaaa")
# f.seek(1)
# print(f.read())
# f.close()
#Detailed function
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# # count = f.read(8) #Read all characters
# f.seek(3) # Locate the cursor position by byte, one English one byte, one Chinese three bytes
# count = f.read()
# print(count)
# f.close()
#Breakpoint resume first locate the cursor position, and then adjust the cursor position
# tell (tell you where the cursor is)
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# f.write("hahahaha")
# count = f.tell()
# f.seek(count-9)
# print(f.read())
# f.close()
#readline
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# line = f.readline() #Read line by line
# print(line)
# f.close()
#readlines Each line is treated as an element in the list and added to the line list
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# line = f.readlines()
# print(line)
# f.close()
#truncate intercept a read
# f = open("The weather is really nice today", mode="r+", encoding="utf-8")
# f .truncate(4)
# f.close()
# open multiple files at the same time
# with open("The weather is nice today",mode="r+",encoding="utf-8") as f :open("The weather is nice today",mode="a",encoding="utf-8") as f1
# print(f.read(),f1.read()
#log in Register
username = input("Username:")
possword = input("Password:")
with open("登录",mode = "w",encoding="utf-8")as f:
f.write("{}\n{}".format(username,possword))
print("registration successful")
list =[]
count = 0
while count < 3:
uname = input("Enter username:")
pwd = input("Please enter your password")
with open("登录",mode = "r+",encoding="utf-8")as f1:
for line in f1:
list.append(line)
if uname == list[0].strip() and pwd ==list[1].strip():
print("Successful login")
break
else:
count += 1
print("Input error")