在当前文件夹下,如果已经存在一个同名的txt文件,那就移除该文件。然后在需要写txt文件之前,通过txt_create = open(‘xxx.txt’, ‘w’, encoding=‘utf-8’)创建一个新的txt文件。
def txt_remove(txt_path):
if os.path.exists(txt_path):
os.remove(txt_path)
print('remove ' + txt_path + ' successed!\n')
else:
print(txt_path + ' not exists!\n')