Get and remove last line from a file

Hi,

how to get the last line from a file and remove this line using Ros scripting?
And second, how to add one line (as first line) to file using Ros scripting?

Thank you.