[vox-tech] Re: vox-tech digest, Vol 1 #843 - 6 msgs

Norm Matloff vox-tech@lists.lugod.org
Tue, 29 Jul 2003 12:37:27 -0700


On Tue, Jul 29, 2003 at 12:00:01PM -0700, vox-tech-request@lists.lugod.org wrote:

> Message: 5
> Date: Tue, 29 Jul 2003 10:46:23 -0700
> From: David Siedband <david@calteg.org>
> To: vox-tech@lists.lugod.org
> Subject: [vox-tech] python: slicing extensions from file names
> Reply-To: vox-tech@lists.lugod.org
> 
> I'd like to find a way to slice the extension off of a file name in python.
> 
> That is, I'd like to slice everything after the first period in the file 
> name.
> 
> I'm renaming the files based on another algorythm, but I need to keep 
> the extension the same.
> 
> I looked at the slice( ) function, but it seems to want me to specify 
> the size of the slice in characters. andI need to allow for extensions 
> with more or less than 3 characters.

Use split().  Here is an example in Python's interactive mode (it would
be the same in batch mode, of course).

% python
Python 2.2.1 (#1, Aug 30 2002, 12:15:30)
[GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> y = "abc.defg"
>>> z = y.split('.')[0]
>>> z
'abc'
>>>

By the way, I can't resist urging all you Perl users out there to switch
to Python. :-)  It's much cleaner and elegant than Perl, and a joy to
program in.  I have a tutorial at

http://heather.cs.ucdavis.edu/~matloff/python.html 

Norm