class txtwr:
def __init__(self, path):
self.path = path
def write(self, mlist):
# 写之前,先检验文件是否存在,存在就删掉
if os.path.exists(self.path):
os.remove(self.path)
# 以写的方式打开文件,如果文件不存在,就会自动创建
file_w_obj = open(self.path, 'w')
for item in mlist:
file_w_obj.writelines(item)
file_w_obj.write('\n')
file_w_obj.close()
def read(self):
# 以读写的方式打开文件,如果文件不存在,就会自动创建
file_wr_obj = open(self.path, 'r')
# 读取txt文件的所有行到list
all_lines = file_wr_obj.readlines()
return all_lines
def owrite(self, mlist):
# 写之前,先检验文件是否存在,存在就删掉
if os.path.exists(self.path):
os.remove(self.path)
file_w_obj = open(self.path, 'w')
for item in mlist:
file_w_obj.writelines(item)
file_w_obj.close()
Python读写txt文件类
最新推荐文章于 2022-04-03 19:29:35 发布