1. File Operations
Model housewife nurse teacher .txt
. 1, the file path: d: \ Model housewife nurse teacher .txt
2, encoding: UFT-GBK ..... 8
. 3, mode of operation: read-only, write-only, append, read, write, read
what encoding stored file, it operates to open what encoding
read-only: R & lt
RB
F = open ( 'log', mODE = 'R & lt', encoding = 'UTF-. 8')
Content = F .read ()
Print (Content)
f.close ()
F = Open ( 'log', MODE = 'RB')
Content reached, f.read = ()
Print (Content)
f.close ()
R & lt reader +
r + b read write (in bytes type)
F = Open ( 'log', mODE = 'r +', encoding = 'UTF-. 8')
Content reached, f.read = ()
Print (Content)
f.write ( '\ I r + n-way written ')
f.close()
Open = F ( 'log', MODE = 'r + b')
Content reached, f.read = ()
Print (Content)
f.write ( '\ I n-r + b writing mode' .encode ( 'utf- . 8 '))
f.close ()
write only: W
WB
content # clear first source file, write
F = Open (' log ', MODE =' W ', encoding =' UTF-. 8 ')
F. write ( 'I deleted previous content to write into')
f.close ()
f = Open ( 'log', the MODE = 'wb')
f.write ( 'I write to delete previous content into 1'.encode ( 'UTF-. 8'))
f.close ()
W +
F = Open ( 'log', MODE = '+ W', encoding = 'UTF-. 8')
f.write ( 'I to remove the preceding write contents into ')
f.seek (0)
Print (F.read())
f.close()
w+b
Open = F ( 'log', MODE = 'W + B')
f.write ( 'I removed foregoing summary write into b'.encode + 1W (' UTF-. 8 '))
f.seek (0)
Print (reached, f.read ())
f.close ()
append a
F = Open ( 'log', MODE = 'a', encoding = 'UTF-. 8')
f.write ( '\ n-I was added into the foregoing added ENTER ')
f.close ()
F = Open (' log ', MODE =' ab & ')
f.write (' \ I was added n-go, preceded by a carriage return, bytes'.encode ( ' . 8-UTF '))
f.close ()
2, encoding
# Str ---> byte encode encoding
# s = 'brother'
# B = s.encode ( 'UTF-. 8')
# Print (B)
#byte ---> STR decode decoded
# s1 = b.decode ( 'UTF-. 8')
# Print (S1)
# = S 'ADF'
# B = s.encode ( 'UTF-. 8')
# Print (B)
# # #byte ---> STR decode decoded
# s1 = b .decode ( 'GBK')
# Print (s1)
3, login, registration
username = input ( 'Please enter a user name you want to register:')
password = the INPUT ( 'Please enter the password you want to register:')
with Open ( 'list_of_info', the MODE = 'w', encoding = 'UTF-8 ') AS F:
f.write (' '. the format (username, password) {} \ {n-})
! Print (' Congratulations, registration is successful ')
LIS = []
I =. 3
the while I> 0:
USN = input ( 'enter your user name:')
pwd = input ( 'enter your password:')
with Open ( 'list_of_info', MODE = 'R & lt +', encoding = 'UTF-. 8') AS F1:
for in f1 Line:
lis.append (Line)
IF == USN LIS [0] .strip () and pwd == LIS [1] .strip ():
Print ( '! Login successful')
BREAK
the else: Print ( 'account ! or password is incorrect, you have a chance% s'% (i-1))
i - = 1
Code:
# f = open('d:\\123.txt',mode='r',encoding='utf-8') # content = f.read() # print(content) # f.close() # r # f = open('log',mode='r',encoding='utf-8') # content = f.read() # print(content) # f.close() # f = open('log',mode='r+',encoding='utf-8') # content = f.read() # print(content) # f.write('\n我是r+方式写入的') # f.close() # f = open('log',mode='r+b') # content = f.read() # print(content) #f.write ( '\ n I r + b writing mode' .encode ( 'UTF-. 8')) # f.close () # RB # F = Open ( 'log', MODE = 'RB') # content reached, f.read = () # Print (content) # f.close () # contents clear first source file, write # F = Open ( 'log', MODE = 'W', encoding = 'UTF -8 ') # f.write (' I removed foregoing summary write into ') # f.close () # F = Open (' log ', MODE =' + W ', encoding =' UTF-. 8 ') # f.write ( 'I'd better go write delete the previous content') # f.seek (0) # Print (f.read ()) # f.close() # wb # f = open('log',mode='wb') #f.write ( 'I removed foregoing summary write into 1'.encode (' UTF-. 8 ')) # f.close () # F = Open (' log ', MODE =' W + B ') # F .write ( 'I removed foregoing summary write into b'.encode + 1W (' UTF-. 8 ')) # f.seek (0) # Print (reached, f.read ()) # f.close () # a # F = Open ( 'log', MODE = 'a', encoding = 'UTF-. 8') # f.write ( '\ I was added n-go, preceded by a carriage return') # f.close () # ab & # F = Open ( 'log', MODE = 'ab &') # f.write ( '\ I was added n-go, preceded by a carriage return, bytes'.encode (' UTF-. 8 ')) # f.Close () # of Function # F = Open ( 'log', MODE = 'R & lt +', encoding = 'UTF-. 8') #print ( 'File name:', F.Name) # # reached, f.read Content = (. 3) are read out character # # f.seek (. 3) # of bytes is specified in accordance with the cursor position # # f.tell ( ) # tell you the cursor position, return int value # Print (f.tell ()) # Content reached, f.read = () # Print (Content) # f.readable () is readable # # Line = f.readline () # read line by line '' ' I = 0 the while I <2: I + =. 1 line = f.readline () IF line: Print (line.strip ()) the else: BREAK ' '' # line = f.readlines () # each line as a list of elements added to the list of '' ' Print (of the type (line), line) for li in line: print(type(li),li.strip()) ''' # f.truncate(20) # 在文件中取多少,就是多少,其他的删除 # for line in f: # print(line) # f.close() # f = open('log',mode='a+',encoding='utf-8') # f.write('佳琪') # count = f.tell() # f.seek(count-count) # print(f.read()) # f.close() """ with open('log',mode='r+',encoding='utf-8') as f,\ open('log2',mode='a+',encoding='utf-8') as f1,open('log3',mode='a+',encoding='utf-8') as f3: # f.the Read () # f1.seek (0) # f1.read () # f.write ( 'I want to enter a carriage return') Print # (f1.read ()) # f1.write ( 'I have to enter a carriage return') ) # f.seek(0) # f1.seek(0) # f_1 = f.read () # f_2 = f1.read () # f3.truncate (0) # F3. Write (F_1) # f3.write ( '\ n-') # f3.write (F_2) "" " with Open ( ' beauty .jpg ' , MODE = ' R & lt + B ' ) AS F1, \ Open ( ' beauty 1 .jpg ' , MODE = ' R & lt + B ' ) AS F2, Open ( ' beauty 2.jpg ' , MODE = ' R & lt + B ' ) F3 AS: # F_1 = F1.read() # f_2 = f2.read() # f3.write(f_1+f_2) # f3.write(f_2) f3.seek(0) f1.seek(0) print(f3.read()) print('\n') # print(f1.read())