471,320 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,320 software developers and data experts.

Rename multiple files using names in a text file

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.
Sep 14 '07 #1
2 3380
rémi wrote:
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.
Other than that your strip() is stripping off some whitespace that is
part of the name, I really can't see the problem either, but did you try
to add in the explicit path? E.g.:

path_to = getcwd()
list_files = listdir(path_to)
..
..
..
for nom in list_jpg:
old_path = os.path.join(path_to, nom.strip())
list_jpg_strip.append(old_path)
..
..
..
for old_path, new_name in zip(list_jpg_strip, list_names):
new_path = os.path.join(path_to, new_name)
rename(old_path, new_path)

James
Sep 14 '07 #2
Le Fri, 14 Sep 2007 12:52:52 -0700, James Stroud a écrit:

[...]
Other than that your strip() is stripping off some whitespace that is
part of the name, I really can't see the problem either, but did you try
to add in the explicit path? E.g.:
actually, the problem was in "while i <= len(list_jpg_strip)" and i should
have been "while i < len(list_jpg_strip)".

Antoher problem was the name of jpg's files used for testing. A naughty
one was nammed "file.jpg " instead of "file.jpg". So maube, as you saif,
stripping is useless for list of jpg files.
Thanks a lot.
Rémi
Sep 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.