First, I have to tell you that the data will be separated by one space only, regardless of how many there were in the original file. This is a simple code which has no concerns whatsoever with memory/speed, as it keeps the whole file in memory.
inp = open("somefile", "r")
out = open("anotherfile", "w")
out.write("\n".join(" ".join(reversed(i.rstrip().split())) for i in reversed(inp.readlines())))
inp.close()
out.close()
The fourth line is the one that does the job. Here it is, in a more friendly code block:
#we will produce each line one by one and store here
result = []
for i in reversed(inp.readlines()): # get the lines in reverse fashion
i = i.rstrip() # strip trailing newline
# split the data into a list, then reverse the order and join back with one space separating data
string = " ".join(reversed(i.split()))
#the line is correct, add it to the list
result.append(string)
#join all of the lines, inserting a newline in-between
write_str = "\n".join(result)
#write
out.write(write_str)