strip module bug

Poppy znfmail-pythonlang at yahoo.com
Mon Oct 13 10:31:32 EDT 2008


Thanks Steven and Tim, I understand the strip module a lot more today. Also 
for some reason I was deciding against using the path functions but now 
decided to try and thus implemented them. My script is reading one file and 
writing a new file with a different extension.

So based on your suggestions I wrote this line.

import sys, os
xmlfile = sys.argv[1]
filout = os.path.splitext(xmlfile)[0] + ".xmlparse"  ### here is the new 
line


"Steven D'Aprano" <steve at REMOVE-THIS-cybersource.com.au> wrote in message 
news:01034686$0$20641$c3e8da3 at news.astraweb.com...
> On Mon, 13 Oct 2008 08:50:41 -0400, Poppy wrote:
>
>> I'm using versions 2.5.2 and 2.5.1 of python and have encountered a
>> potential bug. Not sure if I'm misunderstanding the usage of the strip
>> function but here's my example.
>>
>> var = "detail.xml"
>> print var.strip(".xml")   ### expect to see 'detail', but get 'detai'
>> var = "overview.xml"
>> print var.strip(".xml") ### expect and get 'overview'
>
>
> I got bitten by this once too. Most embarrassingly, I already knew the
> right behaviour but when somebody suggested it was a bug I got confused
> and convinced myself it was a bug. It's not.
>
> You have misunderstood what strip() does. It does NOT mean "remove this
> string from the string if it is a suffix or prefix".
>
> Consider:
>
>>>> "abcd123".strip('123')
> 'abc'
>>>> "abcd123".strip('321')
> 'abc'
>>>> "abcd123111".strip('213')
> 'abc'
>
> strip() removes *characters*, not substrings. It doesn't matter what
> order it sees them.
>
> See help(''.strip) in the interactive interpreter for more detail.
>
>
> By the way, the right way to deal with file extensions is:
>
>>>> import os
>>>> os.path.splitext('detail.xml')
> ('detail', '.xml')
>
>
>
>
> -- 
> Steven 





More information about the Python-list mailing list