help with counter
andy
andy at eastonwest.co.uk
Mon Jan 6 18:57:52 EST 2003
On Monday 06 Jan 2003 8:20 pm, Bill Carter wrote:
> I must have been looking at this problem for to long, but I need some
> help because I am stuck.
>
> I have a series of files on disk file.01.txt, file.02.txt ...
> what I am trying to do is rename all the files a new name but also not
> renaming a few files but maintaining the number sequence
>
> for example
>
> #sample command line
> pyrename.py file.01.txt file.400.txt 3 newfile.01.txt
>
> # breakdown
> script name / filename of first file / last filename / drop every 3rd
> file / new filename and start number
>
>
> should produce
>
> file.01.txt newfile.01.txt
> file.02.txt newfile.02.txt
> file.03.txt
> file.04.txt newfile.03.txt
> file.05.txt newfile.04.txt
> file.06.txt newfile.05.txt
> file.07.txt newfile.06.txt
> file.08.txt
> file.09.txt newfile.07.txt
> file.10.txt ... newfile.08.txt...
>
> the filename changes the amount changes and the file to be dropped
> changes.
>
> For some reason I can't put my head around this problem. Any help
> would be appreciated.
>
> BC
Bill some thoughts:
change params to:
start-no finish-no drop-no old-format new-format
use the % operator to mangle the output name
ie:
pyrename.py 1 400 3 file.%i.txt file.%i/txt
#pyrename
import os,string
# get params etc...
start_no=1
finish_no=10
drop_no=3
old_fmt="file.%i.txt"
new_fmt="new.fmt.%i.file"
dirname="/home/andy/pyrename.test"
########### processing starts here #########
# figure out where number is in name
specparts=string.split(old_fmt,".") # get the string bits
numpart=0
for part in specparts:
if part[0]=="%": # this is it
break
else:
numpart+=1
files=os.listdir(dirname) # you supply dirname
# drop unwanted filenames, ie, before start_no, after finish_no and
# modulus drop_no
oldlist=files[:] # new copy of files list
for file in oldlist:
fileparts=string.split(file,".")
num=int(fileparts[numpart])
# obliterate number part in fileparts to make comparison easy
fileparts[numpart]=specparts[numpart]
# remove if unwanted
if fileparts<>specparts: # this file does not match spec
files.remove(file)
elif num<start_no or num>finish_no or (num % drop_no) == 0:
files.remove(file)
# list now only has files we want in it
count=start_no
for file in files:
new_name=new_fmt % count
os.rename(dirname+"/"+file,new_name) # non portable path splice
count+=1
############### end
test:
bash-2.05b$ cd pyrename.test/
bash-2.05b$ touch file.1.txt
bash-2.05b$ touch file.002.txt
bash-2.05b$ touch untouched.333.txt
bash-2.05b$ touch file.003.txt
bash-2.05b$ touch file.004.txt
bash-2.05b$ touch file.005.txt
bash-2.05b$ touch file.006.txt
bash-2.05b$ touch file.007.txt
bash-2.05b$ touch file.008.txt
bash-2.05b$ touch file.009.txt
bash-2.05b$ touch file.10.txt
bash-2.05b$ ls
file.002.txt file.005.txt file.008.txt file.1.txt
file.003.txt file.006.txt file.009.txt untouched.333.txt
file.004.txt file.007.txt file.10.txt
bash-2.05b$ python ../pyrename.py
['file.1.txt', 'file.002.txt', 'file.004.txt', 'file.005.txt', 'file.007.txt',
'file.008.txt', 'file.10.txt']
bash-2.05b$ ls
file.003.txt new.fmt.1.file new.fmt.4.file new.fmt.7.file
file.006.txt new.fmt.2.file new.fmt.5.file untouched.333.txt
file.009.txt new.fmt.3.file new.fmt.6.file
bash-2.05b$
More information about the Python-list
mailing list