Quantcast
Channel: Planet Plone - Where Developers And Integrators Write
Viewing all articles
Browse latest Browse all 3535

Reinout van Rees: Bitten by .rstrip()

$
0
0

I got bitten by an .rstrip() gotcha that I knew about (but that I also had apparently forgotten).

What I wanted to do was to quickly strip the extension from a certain set of files that were named gt.leg, glg.leg and so on. Legend files for rendering geotiff files, if you need to know. I could use the os.path extension stuff, or I could do it the simple way:

>>> 'gt.leg'[:-4]
'gt'
>>> 'glg.leg'[:-4]
'glg'

But I figured using rstrip (strip characters from the right side) would be clearer:

>>> 'gt.leg'.rstrip('.leg')
'gt'
>>> 'glg.leg'.rstrip('.leg')
''

Say again? '' instead of 'glg'?

My error: what you pass to rstrip isn't a fixed string that it removes from the right hand side, but a couple of characters that all are allowed to be removed. and my 'glg.leg' consists completely of all those characters in '.leg'! So all of them got removed.

It was only after I tried it with a couple of other files that the problem dawned on me. I guess I'll remember it now for the next two years at least :-)

Some damage at the Eben Emaël fort in Belgium

Viewing all articles
Browse latest Browse all 3535