阅读文件并反向书写

时间:2015-08-30 01:19:18

标签: python file

我试图读取文件并用反向线写入。到目前为止,这是我的代码:

def write_reversed_file(input_filename, output_filename):
"""Writes a reverse file"""
    with open(input_filename, 'r') as input_file:
        data = input_file.readlines()
    with open(output_filename, 'w') as output_file:
        return output_file.reversed(data)

try:
    write_reversed_file('data.txt', 'reversed.txt')
    print(open('reversed.txt').read())
except IOError:
    print ("Error: can't find file or read data"

1 个答案:

答案 0 :(得分:0)

你不是在问什么!此外,在要求人们修改代码之前,您应该尝试通过自己的方式或在StackOverflow上找到答案。

嗯,你的下半场错了。

 with open(output_filename, 'w') as output_file:
  for i in reversed(data):
    output_file.write(i)