removing extension

W

wilson

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
 
L

Lie

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
 
L

Lie

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"
 
A

Arnaud Delobelle

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

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

More simply, use the rsplit() method of strings:
['C:\\myimages\\imageone', 'jpg']

['C:\\blahblah.blah\\images.20', 'jpg']

HTH
 
M

Matt Nordhoff

Arnaud said:
More simply, use the rsplit() method of strings:
['C:\\myimages\\imageone', 'jpg']

['C:\\blahblah.blah\\images.20', 'jpg']

HTH

There's os.path.splitext(), which probably does just about exactly that.
--
 
M

Marc 'BlackJack' Rintsch

Arnaud said:
More simply, use the rsplit() method of strings:
path = r'C:\myimages\imageone.jpg'
path.rsplit('.', 1)
['C:\\myimages\\imageone', 'jpg']

path = r"C:\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
 
A

Arnaud Delobelle

Marc 'BlackJack' Rintsch said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top