1

Possible Duplicate:
Rename Files in Python

Hey all, I am creating a script in Python to wrap a number of OS commands related to bulk image editing, and most of the tasks are completed, however I am stumped on one task (a seemingly simple one at that).

When I scan pictures on my scanner, I get a filename similar to this:

201105151110_0001A.jpg

where the first part is some sort of a timestamp (which I want to replace), however I wanted to make that an input variable (where I, or other users could just paste that into the command line, if I'm using another scanner where the filename structure could be different. 0001A means the front side of the first photo/document, and I would like to keep that part of the filename.

I have three variables set up:

    old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
    new_prefix = input(bcolors.PROMPT + "Enter the new prefix: " + bcolors.ENDC)
    working_directory

working_directory is the variable from another part of the code, which is where the images would be located. The color part is just so I can colorize the output and make it easier to read. There would be anywhere from 1-1000 files in the directory when I work on it.

This script will be run on Linux.

Thank you!

---EDIT---

Sorry for wasting your time guys, it seems I overlooked some information in the question linked here by Kiril Kirov, the code that I came up with, which works is:

elif retouch_option == "06":
    print(" ")
    old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
    new_prefix = input(bcolors.PROMPT + "Enter the new prefix.......: " + bcolors.ENDC)
    print(bcolors.OUTPUT + " ")
    for fname in glob(working_directory + "*.jpg"):
        keeper = fname[-9:]
        print("Renaming image", keeper)
        os.rename(fname, fname.replace(old_prefix, new_prefix)) 

I think it should be safe, since it is just replacing the old_prefix variable with the new_prefix variable. Is this true? If not I'd definitely appreciate feedback, although this seems to be working fine so far.

Community
  • 1
  • 1
  • so you want to split it like $1_$2.jpg and you want to keep $2 and replace $1? what are you stuck on reading the input, or processing the next step? – matchew May 15 '11 at 17:10
  • `for fn in *.jpg ; do mv -n $fn $(echo $fn | sed 's/.*_//') ; done` – Dietrich Epp May 15 '11 at 18:31
  • I added information to my initial post, and so far it seems to be safe, although I am quite new to Python, so I am not sure if I can hose something by using it like this (i.e., it's not checking if the pattern matches), but since it's just replacing one variable with another, that should work right? – Mylan Connolly May 15 '11 at 18:38
  • Mylan, FB on your edit: your code is a little fragile, in that if the length of the trailing end of the filename changes, it will do the wrong thing (according to your description.) You mentioned that you wanted the solution to work if the filename structure was different, which I take to also mean the trailing end might be a different length. – fyngyrz May 15 '11 at 18:56
  • Thank you for the feedback, I'll check out reworking this code and implementing the changes you suggested after work today. I appreciate the help everybody! – Mylan Connolly May 16 '11 at 11:00

1 Answers1

2

something like:

sep = '_'
try:
    prefix,keeper = filename.split(sep)
except: # filename does not match desired structure
    print "not processed: no '" + sep + "' in '"+ filename + "'"
else:   # split succeeded: 
    if prefix == old_prefix:
        filename = new_prefix + sep + keeper
        # more processing...
    else:
        print "prefix doesn't match in '" + filename + "'"
fyngyrz
  • 2,458
  • 2
  • 36
  • 43
  • 1
    if you use `prefix, sep, rest = filename.partition('_')` then `if prefix == old_prefix: filename = new_prefix + sep + rest` will work even if `filename` doesn't contain `'_'`. – jfs May 15 '11 at 17:51
  • "partition" is only python 2.5 and later. question didn't specify the python version, so I went with something basic. Also, you probably don't *want* this to work if the filename is, for example, '..' – fyngyrz May 15 '11 at 18:23