Rename multiple files using names in a text file

rémi nospam at non.com
Fri Sep 14 14:42:56 EDT 2007


Hi,

I would like to rename files (jpg's ones) using a text file containing the
new names...
Below is the code that doesn't work :
*****
#!/usr/bin/python
#-*- coding: utf-8 -*-
from os import listdir, getcwd, rename
import re
list_names=['new_name1','new_name2']
list_files = listdir(getcwd())
filtre = re.compile("jpg$", re.IGNORECASE) 
list_jpg = filter(filtre.search, list_files)
#strip all element of list list_jpg
list_jpg_strip=[]
for nom in list_jpg:
	#print nom.strip()
	list_jpg_strip.append(nom.strip())
#let's rename :
i=0
while i <= len(list_jpg_strip):
	rename(list_jpg_strip[i],list_names[i])
	i=i+1
****
The error message is :
File "ecm.py", line 17, in <module>
    rename(list_jpg_strip[i],list_names[i])
OSError: [Errno 2] No such file or directory
and all files exists, I checked it hundred times ! :-s
Do you have a clue ?
Thanks a lot.
Rémi.



More information about the Python-list mailing list