请在下面代码中补充,要求保存成txt文件时写入每列的字段名
import os
# 定义要合并的txt文件所在目录
# directory = r'C:\Users\Sam\PycharmProjects\pythonProject\毕设\pachong\pachong_sufei'
# 获取该目录下所有的txt文件名
file_names = [f for f in os.listdir(directory) if f.endswith('.txt')]
# 创建新的合并后的txt文件
merged_file = open('merged01.txt', 'w',encoding='utf-8',errors='ignore')
# 遍历每个txt文件进行读写操作
for file_name in file_names:
with open(os.path.join(directory, file_name), 'r',encoding='utf-8',errors='ignore') as file:
merged_file.write(file.read())
# 关闭合并后的txt文件
merged_file.close()