[Tutor] I need help slicing.

Gregor Lingl glingl@aon.at
Fri, 12 Jul 2002 20:57:51 +0200


> How do you slice a string from rear to front that is variable length?
>
> For example:
>
> I have two strings -
>     test.html
>     test.txt
>
> I would like to compare each string individually and do a conditional
> statement that says how to handle the file. In other words, I would like
my
> script to individually discern between the .txt file and the .html file
and
> handle each file differently based upon it's file extension.
>
Perhaps you can use this approach:

>>> def extension(fn):
         return fn.split('.')[-1]

>>> extension('test.html')
'html'
>>> extension('test.txt')
'txt'
>>> 'root/subdir/file'.split('/')
['root', 'subdir', 'file']
>>>