I'm a bit new and can't configure the rstrip
command to do what I want.
I did some string manipulation on a file which worked great, and I've written the results to a new file. That new file now just needs the trailing whitespace removed from the end of all the lines.
My code that doesn't work goes like this:
fo = open( FILENAME, 'r+' )
data_l = fo.read()
data = data_l.rstrip
fo.write(data)
This gives me an error:
fo.write(data)
TypeError: expected a character buffer object
I'm guessing this is to do with me reading and writing to the same file? I tried looking into this but all the solutions looked verbose and complex and I feel this is probably quite a simple, common task.
So my question is, what's a good way of doing this?
This is one way of doing it, sing a temporary file:
Reading and writing to the same file will be more complicated. You won't gain much (any?) performance because you need to write the data anyway. Just deleting something in the middle of a file means re-writing all the rest of the file content.