我有这样的程序
files_name = ['1.txt', '2.txt', '3.txt']
redacted_file = open('final.txt', 'w+', encoding='utf-8')
xxx = []
for i in range(len(files_name)):
with open(files_name[i], encoding='utf-8') as f:
for j in f:
xxx.append((files_name[i], len(list(f)) + 1, f))
xxx.sort(key=lambda x: x[1])
for j in range(len(xxx)):
redacted_file.write(str(xxx[j][0]) + '\n')
redacted_file.write(str(xxx[j][1]) + '\n')
redacted_file.write(str(xxx[j][2]) + '\n')
这就是 final.txt 中发生的事情
> 2.txt 1 <_io.TextIOWrapper name='2.txt' mode='r' encoding='utf-8'>
> 1.txt 8 <_io.TextIOWrapper name='1.txt' mode='r' encoding='utf-8'>
> 3.txt 9 <_io.TextIOWrapper name='3.txt' mode='r' encoding='utf-8'>
一切都是正确的,剩下的只是将文件对象翻译成文本,这里可以做什么?