Use basic exercises two kinds of ways to modify the function of the file

# 1, prepared by editing the file, the function is called, passing the three parameters (the file path changes, to modify the contents, modified) can modify the finished document 
'' '
Import OS
DEF File (D, Old , new new):
with Open (D, MODE = 'RT', encoding = 'UTF-. 8') AS F1, Open ( 'n.txt', MODE = 'wt', encoding = 'UTF-. 8') AS F2 :
for Line in F1:
f2.write (line.replace (Old, new new))
The os.remove (D)
os.rename ( 'n.txt', D)

File ( 'that girl.txt', 'Girl', 'A Girl')
'' '
# 2, write tool tail
' ''
Import Time
DEF File (File, Times):
with Open (File, MODE = 'RB') AS F:
f.seek (0, 2)
the while true:
I = reached, f.read ()
IF len (I) == 0:
time.sleep(times)
else:
Print (i.decode ( 'UTF-8'), End = '')
'' '
# 3, writing a login function
' ''
DEF the Users (File, the Users, PAS):
with Open (File, the MODE = 'RT' , encoding = 'UTF-8') AS f:
for f Line in:
the User, Word = line.split ( ':')
IF the User and the Users == == PAS Word:
'! Login successful' Print ()
the else:
print ( 'Login failed')

the Users ( 'userinfo.txt', 'Tank', '123')
'' '
# 4, written registration function
' ''
DEF the Users (File, the Users, PAS):
with Open (File, = MODE 'AT', encoding = 'UTF-. 8') AS F:
f.write ( '{}: {} \ n'.format (Users,pas))

users('userinfo.txt','egon','123')
'''

Guess you like

Origin www.cnblogs.com/0B0S/p/12511730.html