473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

removing extension

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
Jun 27 '08 #1
6 1372
Lie
On Apr 27, 5:34 pm, wilson <wilson.t.thomp...@gmail.comwrote:
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
Jun 27 '08 #2
Lie
On Apr 27, 6:05 pm, Lie <Lie.1...@gmail.comwrote:
On Apr 27, 5:34 pm, wilson <wilson.t.thomp...@gmail.comwrote:
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
Jun 27 '08 #3
Lie <Li******@gmail.comwrites:
On Apr 27, 6:05 pm, Lie <Lie.1...@gmail.comwrote:
>>
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 = r"C:\blahblah.blah\images.20.jpg"
path.rsplit('.', 1)
['C:\\blahblah.blah\\images.20', 'jpg']

HTH

--
Arnaud
Jun 27 '08 #4
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 = 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.
--
Jun 27 '08 #5
On Sun, 27 Apr 2008 15:06:54 +0000, 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 = 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
Jun 27 '08 #6
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raghul | last post by:
friends When i execute the python bu dbleclick in windows a command prompt run s at back what to do to stop the command prompt running back when running python. pls give me a solution
1
by: lievemario | last post by:
Hey, I have generated following xml file (using perl,dbi), Now I would like to display this with the help of xsl such that I have the following result: A B (XXXX) (YYYY)
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
10
by: scott | last post by:
Below, I'm trying to remove the querystring name& value of "catID=12". I mananged to isolate the RESULTS part, but I need to be able to strip the querystring name and it's value, no matter if the...
32
by: Stephen | last post by:
Is there a standard way to remove the warning that a C compiler might produce from the statement: if (a = b) {} I don't want to do: if ((a = b) != 0) {} Because my "a = b" is actually...
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
9
by: samatair | last post by:
Hi I need to remove the .php extension using .htaccess from my URL. I have tried different .htaccess code by searching in the net but none is fruitful.. I have the least idea about regular...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.