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 :-)
My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):