-6

Possible Duplicate:
How to change last letter of filename to lowercase if it is a letter?

This post is a follow-up of my previous post. The Answer below was from Abhijit.

It checks the last character of a filename and changes it to lower case if it is a character. I need to adapt it so that it checks the 5th from last character instead. e.g fooB.PNG > foob.PNG

Rob

import fnmatch
import os

def listFiles(dir):
    rootdir = dir
    for root, subFolders, files in os.walk(rootdir):
        for file in files:
            yield os.path.join(root,file)
    return


for f in listFiles(r"N:\test1"):
    if f[-5].isalpha():
        os.rename(f,f[:-5]+f[-5].lower())
        print "Renamed " + f + "to" + f[:-5]+f[-1].lower()
Community
  • 1
  • 1
Robert Buckley
  • 11,196
  • 6
  • 24
  • 25
  • Please be clear and format the question properly. – Yugal Jindle Nov 30 '11 at 17:19
  • This was already answered in OP's last question, in the same answer from which he copied this code. – interjay Nov 30 '11 at 17:27
  • 1
    I am really disheartened. You asked a [similar question](http://stackoverflow.com/questions/8314983/how-to-change-last-letter-of-filename-to-lowercase-if-it-is-a-letter/8315221#8315221), copied my answer and posted a new question here. Did not care to either care to select the answer as acceptable nor cared to look back to my response to your comment. – Abhijit Nov 30 '11 at 17:24
  • 1
    As others have said: duplicate by same user, no credit to author of previous answer – Scott Hunter Nov 30 '11 at 17:30

1 Answers1

0

Do something like this

>>> import os
>>> for root, subFolders, files in os.walk('/tmp'):
...   for f in files:
...     if len(f) < 5: continue
...     newf = f[:-5]+f[-5].lower()+f[-4:]
...     print "changing",f,"to",newf
... 

but looks like you want character before extension instead of 5 char? in that case why not just split the extension from filename and rejoin it after lowering the last char

Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • I tried the last examples but didn´t get a positive result. Sorry! – Robert Buckley Nov 30 '11 at 17:34
  • ok ok!! I have used this forum only a couple of times and have to learn the rules! Please don´t take it personally, I don´t mean to insult anyone – Robert Buckley Nov 30 '11 at 17:42
  • 1
    @RobertBuckley, the rule is universal. You don't take someone else's code without attributing it to them. That's for this site and every other site. –  Nov 30 '11 at 23:07