【发布时间】:2019-12-23 18:45:06
【问题描述】:
我正在尝试写入 txt 文件,但遇到以下错误消息:[Errno 2] No such file or directory: 'results.txt'。 results.csv 文件包含由管道分隔符分隔的数据。我可以知道出了什么问题以及如何解决这个问题吗?
results_path = "results.csv"
dest_path = "results.txt"
def row_count(results_path):
return len(open(results_path).readlines())
def add_header_footer(results_path, dest_path, file_name, date_today):
with open(results_path) as from_file, open(dest_path) as to_file:
header = 'H|ABC|' + file_name + '|' + date_today + '\n'
footer = 'E|' + str(row_count(results_path)) + '|\n'
to_file.write(header)
shutil.copyfileobj(from_file, to_file)
to_file.write(footer)
add_header_footer(results_path, dest_path, 'Results_Today', '20190818')
【问题讨论】: