[Tutor] I need help slicing.

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 12 Jul 2002 11:57:34 -0700 (PDT)


On Fri, 12 Jul 2002, SA wrote:

> How do you slice a string from rear to front that is variable length?

Hi SA,


We can slice from the end of a string like this:

###
>>> secret = "Elbereth"
>>> secret[3:]
'ereth'
>>> secret[4:]
'reth'
>>> secret[5:]
'eth'
>>> secret[5:-1]
'et'
###

So if we leave off the right endpoint of the slice, Python assumes we'll
want to include up to the end of the string.  If you want to see a few
more examples of string slicing, try:

http://www.python.org/doc/tut/node5.html#SECTION005120000000000000000

Slicing works pretty similarly between strings and the other sequences in
Python (like lists and tuples), so once we understand string slicing, we
get the others for free.  *grin*



> 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.
>
>
> Would this be better hadled with re or string slicing?


To get that file extension, we can look for the last position of the '.'
in the string, and slice from there.  Regular expressions will work, but
this pattern is simple enough that it might just be easier to us a simple
rfind() or rindex() to get the last position of that period.

###
>>> filename = "foobar.xml"
>>> filename.rindex('.')
6
>>> filename[6:]
'.xml'
###


And to make this convenient for ourselves, we can put these pieces
together into a function:

###
>>> def file_extension(filename):
...     dot_index = filename.rindex('.')
...     return filename[dot_index :]
...
>>> file_extension('foobar.html')
'.html'
###



> On another note, say it has a directory in front of the file, I would
> like the script to look at the directory name and decide whether it is
> ok or not to continue on working on this file.

Hmmm... If we're doing a lot of path-related things, we may want to look
at the 'os.path' module, which brings a lot of path-related functions
together:

    http://www.python.org/doc/lib/module-os.path.html


Hey, in fact, 'os.path' has a function called 'splitext()' which splits
the extension right out of a filename, just like the file_extension()
function above!  Very nice.  *grin*

Another advantage of using os.path is that it takes care of some
platform-specific stuff.




Best of wishes to you!