change extensions

Bill Mill bill.mill at gmail.com
Tue Apr 5 10:51:14 EDT 2005


On Apr 5, 2005 10:43 AM, Jeffrey Maitland <maitj at vianet.ca> wrote:
> That approach works, but so does this one.
> 
> import os, glob
> 
> input_file_list = glob.glob('*.txt')
> for in_file in input_file_list:
>     name = in_file.split('.')
>     os.rename(in_file, str(name[0]) + '.' + 'text'))
> 

you should really use in_file.splitext - your script renames
myfile.with.lots.of.dots.txt to myfile.text instead of
myfile.with.lots.of.dots.text .

If you *really* wanted to use split(), it oughta be
''.join(in_file.split('.')[:-1]) , but why not use the built-in?

Peace
Bill Mill
bill.mill at gmail.com



More information about the Python-list mailing list