txt写入
#txt_path:txt地址
#wd_info写入的文字
def txtWriter(txt_path, wd_info):
f = open(txt_path, 'a+', encoding = 'utf-8', errors = 'ignore')
f.write(wd_info)
f.close()
#应用:
txt_path = r'd:\txt\1.txt'
wd_info = '输入的文字' + '\n'
txtWriter(txt_path, wd_info)
txt读取
f = open(filePath, 'r')
content = f.readlines()
for line in content:
print(line)
读取txt可能遇到的问题
- 如果txt读取的文本信息出现乱码,可以尝试使用如下方式读取:
f = open(filePath, encoding='utf-8', errors='ignore').read()