Q:

Python: Reading a file and reverse all lines

So I'm trying to reverse all lines, but instead I get totaly strange reverse, it puts 4th line on the 1st line with the 5 line...
How file looks before reversing:
1 line
2 line
3 line
4 line
5 line

After reversing:
5 line4 line
3 line
2 line
1 line

Here is the code:
    file = open(filename, "r")
    list = file.readlines()
    file.close()

    list.reverse()

    file = open(filename, "w")
    for value in list:
        file.write(value)
    file.close()

A:

It seems that you didn't have the end line sign in the last line (the '\n').
It could be fixed easily by adding '\n' when needed:
file = open(filename, "w")
    for value in list:
         file.write(value.rstrip() + "\n")
    file.close()

