Rename multiple files using names in a text file

Discussion in 'Python' started by =?iso-8859-1?b?cultaQ==?=, Sep 14, 2007.

  1. 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,list_names)
    i=i+1
    ****
    The error message is :
    File "ecm.py", line 17, in <module>
    rename(list_jpg_strip,list_names)
    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.
    =?iso-8859-1?b?cultaQ==?=, Sep 14, 2007
    #1
    1. Advertising

  2. =?iso-8859-1?b?cultaQ==?=

    James Stroud Guest

    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,list_names)
    > i=i+1
    > ****
    > The error message is :
    > File "ecm.py", line 17, in <module>
    > rename(list_jpg_strip,list_names)
    > 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
    James Stroud, Sep 14, 2007
    #2
    1. Advertising

  3. 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
    =?iso-8859-1?b?cultaQ==?=, Sep 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    950
    M.E.Farmer
    Feb 13, 2005
  2. error when rename pointer names

    , Jul 17, 2006, in forum: C Programming
    Replies:
    3
    Views:
    274
    Keith Thompson
    Jul 18, 2006
  3. Tom Bates
    Replies:
    5
    Views:
    192
    Chris Mattern
    Apr 25, 2005
  4. Terry
    Replies:
    7
    Views:
    222
    Ted Zlatanov
    Aug 1, 2006
  5. Peter Jamieson

    Rename files using directory names

    Peter Jamieson, Nov 15, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    167
    John W. Krahn
    Nov 16, 2007
Loading...

Share This Page