[Tutor] improvements on a renaming script

street.sweeper at mailworks.org street.sweeper at mailworks.org
Sun Mar 9 20:22:35 CET 2014


Hello all,

A bit of background, I had some slides scanned and a 3-character
slice of the file name indicates what roll of film it was.
This is recorded in a tab-separated file called fileNames.tab.
Its content looks something like:

p01     200511_autumn_leaves
p02     200603_apple_plum_cherry_blossoms

The original file names looked like:

1p01_abc_0001.jpg
1p02_abc_0005.jpg

The renamed files are:

200511_autumn_leaves_-_001.jpeg
200603_apple_plum_cherry_blossoms_-_005.jpeg

The script below works and has done what I wanted, but I have a
few questions:

- In the get_long_names() function, the for/if thing is reading
the whole fileNames.tab file every time, isn't it?  In reality,
the file was only a few dozen lines long, so I suppose it doesn't
matter, but is there a better way to do this?

- Really, I wanted to create a new sequence number at the end of
each file name, but I thought this would be difficult.  In order
for it to count from 01 to whatever the last file is per set p01,
p02, etc, it would have to be aware of the set name and how many
files are in it.  So I settled for getting the last 3 digits of
the original file name using splitext().  The strings were unique,
so it worked out.  However, I can see this being useful in other
places, so I was wondering if there is a good way to do this.
Is there a term or phrase I can search on?

- I'd be interested to read any other comments on the code.
I'm new to python and I have only a bit of computer science study,
quite some time ago.


#!/usr/bin/env python3

import os
import csv

# get longnames from fileNames.tab
def get_long_name(glnAbbrev):
	with open(
		  os.path.join(os.path.expanduser('~'),'temp2','fileNames.tab')
		  ) as filenames:
		filenamesdata = csv.reader(filenames, delimiter='\t')
		for row in filenamesdata:
			if row[0] == glnAbbrev:
				return row[1]

# find shortname from slice in picture filename
def get_slice(fn):
	threeColSlice = fn[1:4]
	return threeColSlice

# get 3-digit sequence number from basename
def get_bn_seq(fn):
	seq = os.path.splitext(fn)[0][-3:]
	return seq

# directory locations
indir = os.path.join(os.path.expanduser('~'),'temp4')
outdir = os.path.join(os.path.expanduser('~'),'temp5')

# rename
for f in os.listdir(indir):
	if f.endswith(".jpg"):
		os.rename(
			os.path.join(indir,f),os.path.join(
				outdir,
				get_long_name(get_slice(f))+"_-_"+get_bn_seq(f)+".jpeg")
				)

exit()


Thanks!


More information about the Tutor mailing list