[Tutor] renumbering a sequence

Kent Johnson kent37 at tds.net
Sun Apr 5 02:05:41 CEST 2009


On Sat, Apr 4, 2009 at 3:37 PM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
>
> I want to write a script that takes a list of images and renumbers them with a user supplied number.  Here is a solution I came up while noodling around in the interpreter:
>
>>>> alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"]
>>>> new_start = 5000

You might want to sort the list, depending on where it came from:
alist.sort()

>>>> for x in alist:
> ...     name, number, ext = x.split(".")
> ...     new_frame = name + "." + str(new_start) + "." + ext

You could use string formatting here;
new_frame = '%s.%s.%s' % (name, new_start, ext)

> ...     new_start = new_start + 1

new_start += 1

> ...     print new_frame
> ...
> frame.5000.tif
> frame.5001.tif
> frame.5002.tif
>
> How is that for a solution?  Is there a more elegant way to solve this problem?

It's fine.

Kent


More information about the Tutor mailing list