python Lesson Eight

# F = open ( 'f: \ Model class housewife nurse .txt', MODE = 'R & lt', encoding = 'UTF-. 8') 
# reached, f.read Content = ()
# Print (Content)
# f.close ()

for w # go without this file to create a file
# F = Open ( 'log', MODE = 'w', encoding = 'UTF-. 8')
# f.write Content = ( 'love tonight')
# Print (Content )
# f.close ()
# # clear first source file contents, write
# F = Open ( 'log', MODE = 'W', encoding = 'UTF-. 8')
# f.write content = ( ' I love you, Kai Li ')
# Print (Content)
# f.close ()
# f = Open (' log ', the MODE =' wb ')
# = f.write Content (' I love you very much Kai Li '.encode ( 'UTF-. 8'))
# Print (Content)
# f.close ()
# additionally
# F = Open ( 'log', MODE = 'A', encoding = 'UTF-. 8')
# f.write ( 'Tonight love it! ')
# F.close ()
# write
# f = open (' log ' , mode =' r + ', encoding =' utf-8 ')
Print # (reached, f.read ())
# f.write ( 'Mary, love you')
# f.close ()

# = F Open ( 'log', MODE = '+ R & lt B')
# Print (F. Read ())
# f.write ( 'Mary, love you' .encode ( 'UTF-. 8'))
# f.close ()

# = F Open ( 'log', MODE = '+ W', encoding = ' 8-UTF ')
# f.write (' I want you Kai Li ')
# f.seek (0)
# Print (f.read ())
# f.close ()
# f = Open (' log ', the MODE = 'w + b')
# f.write ( 'I want you Kai Li' .encode ( 'UTF-8'))
# f.seek (0)
# Print (f.read ())
# f.close ( )

# = F Open ( 'log', MODE = '+ A', encoding = 'UTF-. 8')
# f.write ( 'we love it!')
# f.seek (0)
# Print (reached, f.read ( ))
# f.close ()
# of function
f = open ( 'log ', MODE =' R & lt + ', encoding =' UTF-. 8 ')
# reached, f.read Content = (12 is) are read out character #
# F.seek (10) # of bytes is specified in accordance with the cursor position
to tell your cursor position f.tell # () #
# reached, f.read Content = ()
# Print (Content)
# f.tell ()
# F. readline () # whether the mark
# line = f.readline () # read line by line
# line = f.readlines () # each line as an element in the list is added to the list
# f.truncate (5) # taken
Line F in for #:
# Print (Line)
# f.close ()
#
# with Open ( 'log', MODE = 'R & lt +', encoding = 'UTF-. 8') AS obj, Open ( 'log', MODE = '+ W', encoding = 'UTF-. 8') AS F1:
# Print (obj.read ())

username = iNPUT ( 'enter the user you want to register:')
password = iNPUT ( 'Please input register password: ')
with Open (' log ', MODE =' W ', encoding =' UTF-. 8 ') AS F:
f.write (' {} \ {n-} 'the format (username, password.))
Print ('registration success')
lis=[]
i=0
while i< 3:
usn = input ( 'Enter your user name:')
pwd = INPUT ( 'Enter your password:')
with Open ( 'log', MODE = 'R & lt +', encoding = 'UTF-. 8') AS F1 :
for Line in f1:
lis.append (Line)
IF == USN LIS [0] .strip () and pwd == LIS [1] .strip ():
Print ( 'successful landing')
BREAK
the else: Print ( ' account number and password error ')
I + =. 1

# File 
# open file
#open ( 'path', 'Open', 'specify the encoding')
# Open W + A + R & lt RWA +
# + R & lt open file read and write directly write
# encoding --utf-8
Action file #
# read
# read a tail read
# readline read line by line
# does not know where the end of the
# video pictures rb bytes in bytes read
#for cycle --- the best
# write the write
# file pointer cursor ----
# seek to a specified cursor position
#tell current cursor position taken
#truncate taken file
# Close Close file
# modify the file
# file is not modified
with open ( 'log', encoding = 'utf-8') as f, Open ( 'Iog2', 'W', encoding = 'UTF-. 8') AS F2:
for in Line F:
if 'Gong Qili' in line:
line = line.replace ( 'Gong Qili', 'Kai Li')
# write file
f2.write (Line)
# delete files and rename files
Import os
os.remove ( 'log') # delete files
os.rename ( 'log ',' log2 ') # renaming files

Guess you like

Origin www.cnblogs.com/huangjianfeng/p/11223664.html