文件的两种修改方式、函数的基本使用练习

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
'''
import os
def file(d,old,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))
os.remove(d)
os.rename('n.txt',d)

file('that girl.txt','girl','a girl')
'''
# 2、编写tail工具
'''
import time
def file(file,times):
with open(file, mode='rb') as f:
f.seek(0, 2)
while True:
i = f.read()
if len(i) == 0:
time.sleep(times)
else:
print(i.decode('utf-8'), end='')
'''
# 3、编写登录功能
'''
def users(file,users,pas):
with open(file,mode='rt',encoding='utf-8') as f:
for line in f:
user,word = line.split(':')
if users == user and pas == word:
print('登录成功!')
else:
print('登录失败')

users('userinfo.txt','tank','123')
'''
# 4、编写注册功能
'''
def users(file,users,pas):
with open(file,mode='at',encoding='utf-8') as f:
f.write('{}:{}\n'.format(users, pas))

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

猜你喜欢

转载自www.cnblogs.com/0B0S/p/12511730.html
今日推荐