removing extension

Lie Lie.1296 at gmail.com
Sun Apr 27 07:09:33 EDT 2008


On Apr 27, 6:05 pm, Lie <Lie.1... at gmail.com> wrote:
> On Apr 27, 5:34 pm, wilson <wilson.t.thomp... at gmail.com> wrote:
>
>
>
> > i was trying to convert all images in a folder to another type and
> > save the new images in a separate folder.for that i wrote a class and
> > coded some part
>
> > class ConvertImgs:
> >         def __init__(self,infldr,outfldr):
> >                 if os.path.isdir(infldr):
> >                         self.infldr=infldr
> >                         self.outfldr=outfldr
> >                 else:
> >                         print "no such folder,exits program"
> >                         exit(1)
> >                 if not os.path.isdir(self.outfldr):
> >                         os.mkdir(self.outfldr)
> >                         print "made:",self.outfldr
>
> >                 for x in os.listdir(infldr):
> > self.origlist=[os.path.normpath(os.path.join(self.infldr,x)) for x in
> > os.listdir(infldr)]
>
> > ...
> > the self.origlist  returns a list of filenames in infolder.I would
> > like to get them as 'C:\\myimages\\imageone'  instead of 'C:\\myimages\
> > \imageone.jpg' sothat i can add a diff extension to all those strings
> > in the list and save in diff format(ie change 'C:\\myimages\\imageone'
> > to 'C:\\myimages\\imageone.gif ' and save in gif format).but i don't
> > know how to remove those extension from the namestring ..can someone
> > help?
> > W
>
> I don't know if this is the simplest way, but you can use re module.
>
> import re
> pat = re.compile(r'(.*?)\..*')

Sorry, this line should be:
pat = re.compile(r'(.*)\..*')

or paths like these wouldn't pass correctly:
"C:\\blahblah.blah\\images.20.jpg"

> name = pat.search('C:\\myimages\\imageone.jpg').group(1)
> print name




More information about the Python-list mailing list