file_name_fixer.py

Steven D'Aprano steve at REMOVEMEcyber.com.au
Wed Jan 25 03:27:44 EST 2006


ewaguespack at gmail.com wrote:

> i put this together to fix a bunch of files with wierd names, please
> gimme feedback, i am a newbie

Others have already made comments, here is some more 
food for thought.

You should consider factoring out some repeated code 
into functions. E.g.:

# warning: untested!!!
def replace_all(s, old, new):
     """Replaces all instances of substring old with
     substring new."""
     if old == new:
         # make no changes
         return s
     elif old in new:
          raise ValueError("old substring can't be "
              "part of the replacement substring.")
     while old in s:
         s = s.replace(old, new)
     return s

Now you can call it in your loop:

> for path, subdirs, files in os.walk(dir, topdown=True):
>     oldname = path
>     newname = oldname.translate(chars)
>     newname = string.lower(newname)

>     while string.count(newname, "__") > 0:
>         newname = string.replace(newname,"__","_")
>     while string.count(newname, "..") > 0:
>         newname = string.replace(newname,"..",".")

becomes:

newname = replace_all(newname, "__", "_")
newname = replace_all(newname, "..", ".")

>     if oldname != newname:
>         if os.path.isfile(newname) or os.path.isdir(newname):
>             print oldname, "-->\n", newname, "\t\t\tERROR: file/dir
> exists\n"
>         else:
>             print oldname, "-->\n", newname, "\t\t\tYAY: file
> renamed\n"
>             if noworky == "doit":
>                 os.renames(oldname, newname)
> print "# PHASE II, FILES"
> for path, subdirs, files in os.walk(dir, topdown=True):
>     for oldname in files:
>         oldname = os.path.join(path, oldname)
>         newname = oldname.translate(chars)
>         newname = string.lower(newname)

More refactoring:

>         newname = string.replace(newname,".mpeg",".mpg")
>         newname = string.replace(newname,".ram",".rm")
>         newname = string.replace(newname,".jpeg",".jpg")
>         newname = string.replace(newname,".qt",".mov")

becomes:

# warning: untested!!!
def fix_extension(s, old, new):
     # there are other, better ways of doing this
     # see the os.path.splitext function
     if s.endswith(old):
         s = s[:-len(old)] + new
     return s

def fix_all_extensions(s, extensions):
     for old, new in extensions:
         s = fix_extension(s, old, new)
     return s


newname = fix_all_extensions(newname,
     [ (".mpeg", ".mpg"), (".ram", ".rm"),
     (".jpeg", ".jpg"), (".qt", ".mov") ]


>         while string.count(newname, "__") > 0:
>             newname = string.replace(newname,"__","_")
>         while string.count(newname, "..") > 0:
>             newname = string.replace(newname,"..",".")

We've already refactored those calls:

newname = replace_all(newname, "__", "_")
newname = replace_all(newname, "..", ".")



That will do for starters.


-- 
Steven.




More information about the Python-list mailing list