Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Rename multiple files using names in a text file

Reply
Thread Tools

Rename multiple files using names in a text file

 
 
=?iso-8859-1?b?cultaQ==?=
Guest
Posts: n/a
 
      09-14-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.
 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      09-14-2007
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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?b?cultaQ==?=
Guest
Posts: n/a
 
      09-15-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename files using directory names Peter Jamieson Perl Misc 4 11-16-2007 02:25 AM
How do I rename a group of files using a list of predefined names? cedarson@gmail.com Computer Support 5 08-15-2006 08:45 PM
how do you rename files with long names? murphdrag Computer Support 6 02-13-2005 11:26 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Rename file names at once (200 files) Adi Computer Support 1 09-09-2004 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57