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 6 1372
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
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
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
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.
--
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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)
|
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,...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |