Beside what Peter has described, one more hint: you probably need to
close your file before calling the rename method.

You can close your file by calling x.close