I have a script for renaming files, but can not seem to iron out the bugs.

I am trying to rename a list of files:

File1.File1
File2.File2

etc

I need them to be renamed to:

File1
File2
etc

This is the code so far:

import os
import sys
import time



    
def renamer(target) :
    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        i = filename.split(".")
        if len(i) == 2 and i[-1] == i[-2]: 
            new_filename = i[1]
            os.rename(filename, new_filename)
            print "Renamed " + filename + " to " + new_filename
            
def main() :
    renamer(sys.argv[1])
    main()

I would like to improve on this, by adding in the ability to copy one last file, and rename it so that:

Copyme

becomes:

NewCopyme

any help would be much appreciated.

Thanks

you can use splitext from os module

import os,shutil    
def renamer(target) :    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        newfilename = os.path.splitext(filename)[0] 
        print newfilename
        os.rename(filename, newfilename)
        print "Renamed " + filename + " to " + new_filename
    shutil.copy("copyme","newcopyme"

you can use splitext from os module

import os,shutil    
def renamer(target) :    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        newfilename = os.path.splitext(filename)[0] 
        print newfilename
        os.rename(filename, newfilename)
        print "Renamed " + filename + " to " + new_filename
    shutil.copy("copyme","newcopyme"

I will give that a try, do I need the def=main etc as well, or can I leave that out??

Thanks you

I will give that a try, do I need the def=main etc as well, or can I leave that out??

Thanks you

it's up to you. however its good to modularize so main() is definitely good to have.

Don't use main the way it was written:

def main() :
    renamer(sys.argv[1])
    main()

It might call itself until the recursion limits are reached.

Use it like this:

def main() :
    renamer(sys.argv[1])

main()

Actually the best way to do this is as follows:

import os,shutil 
   
def renamer(target) :    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        newfilename = os.path.splitext(filename)[0] 
        print newfilename
        os.rename(filename, newfilename)
        print "Renamed " + filename + " to " + new_filename
    shutil.copy("copyme","newcopyme"

# test the function/module
if __name__ == __main__:
    renamer(sys.argv[1])

This test allows the program to be importable module or runnable program.

How would i specify a specific folder within this script? for example a folder called "cars" in "My pictures".

it is a free script and i am trying to under stand it, so i dont nuke all my jpeg files.

it takes all files from the ragex and renames them cars1, cars2, cars 3 ect, but i need to be able to apply this to only a specific folder.

import re, os
rxin = raw_input('enter a regex to search for:\n')
foo = re.compile(rxin)
newname = raw_input('enter a new base name:\n')
a = 0
for fname in os.listdir(os.getcwd()):
allowed_name = re.compile(rxin).match
if allowed_name(fname):
# newfname = string.lower(re.sub(foo,
# '', fname))
# b = (newname + str(a))
a += 1
c = os.path.splitext(fname)
b = (newname + str(a) + c[1])
os.rename(fname, b)

Edited 7 Years Ago by Archenemie: Added relevant information.

You can write

from os.path import join as pjoin
targetfolder = raw_input('enter a target folder:\n').strip()
newname = raw_input('enter a new base name:\n')
newname = pjoin(targetfolder, newname)
...

If you don't want to nuke your jpegs, make a zip file with the whole folder before doing anything.

This question has already been answered. Start a new discussion instead.