Re: removing extension
On Apr 27, 5:34 pm, wilson [EMAIL PROTECTED] 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'(.*?)\..*') name = pat.search('C:\\myimages\\imageone.jpg').group(1) print name -- http://mail.python.org/mailman/listinfo/python-list
Re: removing extension
On Apr 27, 6:05 pm, Lie [EMAIL PROTECTED] wrote: On Apr 27, 5:34 pm, wilson [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: removing extension
Lie [EMAIL PROTECTED] writes: On Apr 27, 6:05 pm, Lie [EMAIL PROTECTED] wrote: 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 simply, use the rsplit() method of strings: path = r'C:\myimages\imageone.jpg' path.rsplit('.', 1) ['C:\\myimages\\imageone', 'jpg'] path = rC:\blahblah.blah\images.20.jpg path.rsplit('.', 1) ['C:\\blahblah.blah\\images.20', 'jpg'] HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: removing extension
Arnaud Delobelle wrote: More simply, use the rsplit() method of strings: path = r'C:\myimages\imageone.jpg' path.rsplit('.', 1) ['C:\\myimages\\imageone', 'jpg'] path = rC:\blahblah.blah\images.20.jpg path.rsplit('.', 1) ['C:\\blahblah.blah\\images.20', 'jpg'] HTH There's os.path.splitext(), which probably does just about exactly that. -- -- http://mail.python.org/mailman/listinfo/python-list
Re: removing extension
On Sun, 27 Apr 2008 15:06:54 +, Matt Nordhoff wrote: Arnaud Delobelle wrote: More simply, use the rsplit() method of strings: path = r'C:\myimages\imageone.jpg' path.rsplit('.', 1) ['C:\\myimages\\imageone', 'jpg'] path = rC:\blahblah.blah\images.20.jpg path.rsplit('.', 1) ['C:\\blahblah.blah\\images.20', 'jpg'] HTH There's os.path.splitext(), which probably does just about exactly that. Not exactly. In the case of no extension `os.path.splitext()` still works: In [14]: 'foo/bar.txt'.rsplit('.') Out[14]: ['foo/bar', 'txt'] In [15]: 'foo/bar'.rsplit('.') Out[15]: ['foo/bar'] In [16]: os.path.splitext('foo/bar') Out[16]: ('foo/bar', '') Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: removing extension
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes: Not exactly. In the case of no extension `os.path.splitext()` still works: In [14]: 'foo/bar.txt'.rsplit('.') Out[14]: ['foo/bar', 'txt'] In [15]: 'foo/bar'.rsplit('.') Out[15]: ['foo/bar'] In [16]: os.path.splitext('foo/bar') Out[16]: ('foo/bar', '') And crucially, it still works correctly if the file has no extension but a directory in the path has one: 'foo.baz/bar'.rsplit('.') ['foo', 'baz/bar'] os.path.splitext('foo.baz/bar') ('foo.baz/bar', '') Conclusion: forget about str.rsplit() (which I suggested), use os.path.splitext() instead :) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list