Results 1 to 10 of 10

Thread: Python Script For Batch Renaming

  1. #1
    Join Date
    Nov 2009
    Beans
    187
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Python Script For Batch Renaming

    I'm trying to write a python script for renaming all of my movie files. What I want to use two text files, one with the names of the filenames of the movies and one with what I want the new name to be, that way I can rename them how I want. I want it to take each line from list_one and rename it to the corresponding line in list_two. Here's what I have so far.

    Code:
    #!/usr/bin/env python
    
    import os
    
    list_one = open('list_one.txt', 'r');
    list_two = open('list_two.txt', 'r');
    
    for line in list_one.readlines():
        new_name = list_two.readlines();
        if os.system('mv ' , line , ' ' + new_name) == 0:
            print line + ' has been rename to ' + new_name;
        else:
            print 'error'
    list_one.close()
    list_two.close()
    The error I get is 'cannot concatenate 'str' and 'list' objects.' A google search reveal many ways to convert lists into single strings, which would print out all of my movies at once, but I could not find how to do what I need to do.

    After that didn't work, I tried this
    Code:
    #!/usr/bin/env python
    
    import os
    
    list_one = open('list_one.txt', 'r');
    list_two = open('list_two.txt', 'r');
    
    for line in list_one.readlines():
        new_name = list_two.readlines();
        if os.rename( line , new_name) == 0:
            print line + ' has been rename to ' + new_name;
        else:
            print 'error'
    list_one.close()
    list_two.close()
    Here I got the error 'TypeError: coercing to Unicode: need string or buffer, list found
    TypeError:: command not found.' From what I read, os.rename() might not be what I want anyways, because if there is an object with the same name (where there might be in my case) it will be removed silently.

    Any suggestions?

  2. #2
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python Script For Batch Renaming

    First of all, why you complicate yourself with two files? Why not just:

    Code:
    "old name", "new name"
    Second, "something.readlines()" loads the whole content of the file to memory, therefore, you get that error, what you want to do is

    PHP Code:
    for (oldname,newnamein zip(oldfilenewfile):
        ... 
    So you can iterate in both files at the same time, and without having to load everything to memory, just buffering.
    Last edited by Can+~; November 21st, 2009 at 09:38 PM. Reason: better names for variables
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  3. #3
    Join Date
    Nov 2009
    Location
    Brighton, UK
    Beans
    21
    Distro
    Xubuntu 9.10 Karmic Koala

    Re: Python Script For Batch Renaming

    Haven't the foggiest on how to do this in Python. But in Perl sudo:

    Code:
    #!/usr/bin/perl
    use warnings;
    use strict;
    open (MYFILE, 'whatever.text');
    while (<MYFILE>) {
    chomp;
    print "$_\n";
    }
    close (MYFILE);
    Your stuff would
    Go
    Here
    open (MYFILE, 'data.txt');
    while (<MYFILE>) {
    chomp;
    print "$_\n";
    close (MYFILE);
    I think this should be correct.. though, you probably Don't know perl if your working with Python

  4. #4
    Join Date
    Nov 2009
    Beans
    187
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Python Script For Batch Renaming

    Quote Originally Posted by Can+~ View Post
    First of all, why you complicate yourself with two files? Why not just:

    Code:
    "old name", "new name"
    Second, "something.readlines()" loads the whole content of the file to memory, therefore, you get that error, what you want to do is

    PHP Code:
    for (oldname,newnamein zip(oldfilenewfile):
        ... 
    So you can iterate in both files at the same time, and without having to load everything to memory, just buffering.
    Yeah, I figured out about the readlines() problem, now I have been trying it with readline() instead, and have been having other problems.

    The reason I am using 2 files is because I have a directory with 300+ movies in it, and it is a lot easier for me to list the contents of the directory into a text file and then change the names into something I want since the movies have no naming convention at all. But I am up for any suggestions, if you have an easier way to do it.

    Sorry for my ignorance, but what does zip() do?

    And no, I don't know anything about perl.
    Last edited by HalfEmptyHero; November 21st, 2009 at 10:06 PM.

  5. #5
    Join Date
    Nov 2009
    Beans
    187
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Python Script For Batch Renaming

    I have been trying this:

    Code:
    list_one = list_one_f.readline()
    list_two = list_two_f.readline()
    while True:
        if list_one = '':
            break;
        elif os.system('mv ' + list_one + list_two) == 0:
            print list_one + 'renamed to ' + list_two
            list_one = list_one_f.readline()
            list_two = list_two_f.readline()
        else:
            print 'error...'
            break
    And it is looking like it might work. The only problem is that 'mv ' + list_one + list_two is looking like this

    Code:
    python renamer.py 
    mv file_one.txt
     file_1.txt
    
    Done.
    So the mv command is not working. Any way to remove the new line that seems to be tacked onto it?
    Last edited by HalfEmptyHero; November 21st, 2009 at 10:30 PM.

  6. #6
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Python Script For Batch Renaming

    Quote Originally Posted by HalfEmptyHero View Post
    I have been trying this:

    Code:
    list_one = list_one_f.readline()
    list_one.strip()
    list_two = list_two_f.readline()
    list_two.strip()
    while True:
        if list_one = '':
            break;
        elif os.system('mv ' + list_one +" "+ list_two) == 0:
            print list_one + 'renamed to ' + list_two
            list_one = list_one_f.readline()
            list_two = list_two_f.readline()
        else:
            print 'error...'
            break
    And it is looking like it might work. The only problem is that 'mv ' + list_one + list_two is looking like this

    Code:
    python renamer.py 
    mv file_one.txt
     file_1.txt
    
    Done.
    So the mv command is not working. Any way to remove the new line that seems to be tacked onto it?
    Fix'd

  7. #7
    Join Date
    Nov 2009
    Beans
    187
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Python Script For Batch Renaming

    Excellent, that is exactly what I needed. As of right now, the script works. The only problem is, after it is done renaming the files it doesn't end, i'm not sure if it is doing anything or not (it isn't renaming files though).

    Code:
    #!/usr/bin/env python
    
    import os
    
    list_one_f = open('list_one.txt', 'r');
    list_two_f = open('list_two.txt', 'r');
    
    list_one = list_one_f.readline()
    list_one = list_one.strip()
    list_two = list_two_f.readline()
    list_two = list_two.strip()
    
    while True:
        if list_one == list_two:
            list_one = list_one_f.readline()
            list_one = list_one.strip()
            list_two = list_two_f.readline()
            list_two = list_two.strip()
        elif os.rename( list_one , list_two) == 0:
            print list_one + ' renamed to ' + list_two
            list_one = list_one_f.readline()
            list_one = list_one.strip()
            list_two = list_two_f.readline()
            list_two = list_two.strip()
        else:
            print 'error...'
            break
    
    print 'Done.'
    list_one_f.close()
    list_two_f.close()
    Any way to make it know when to stop, like when it gets to the end of the file?

    Edit: As I thought, the script keeps comparing the lines on list_one to list_two.
    Last edited by HalfEmptyHero; November 21st, 2009 at 11:44 PM.

  8. #8
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python Script For Batch Renaming

    Instead of calling mv directly, use "os.rename"

    Sorry for my ignorance, but what does zip() do?
    It joins iterable elements into a main iterable of tuples.

    PHP Code:
    >>> = [1234]
    >>> 
    "abcd"
    >>> zip(ab)
    [(
    1'a'), (2'b'), (3'c'), (4'd')] 
    In fact, you can make a dictionary from this:

    PHP Code:
    >>> dict(zip(ab))
    {
    1'a'2'b'3'c'4'd'
    Last edited by Can+~; November 21st, 2009 at 11:47 PM.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  9. #9
    Join Date
    Jun 2007
    Beans
    27

    Re: Python Script For Batch Renaming

    Problem is that you are trying to iterate through one line at a time for infinity (while True) and not stopping it anywhere. When readline() finds the end of the file an empty string is returned (""), you could use that to check whether or not you have reached the end. Then break out of the while loop if a "" is found.

    Consider this quickly baked code snippet for an alternative way to make your batch renamer using the zip method mentioned by Can+~ before.

    If you call the script from command line like this. Assuming it is saved as "renamer" (without .py) and made executable in your current working dir.

    PHP Code:
    $ ./renamer old_filenames.txt new_filenames.txt 
    PHP Code:
    #!/usr/bin/env python
    import ossys

    if __name__ == "__main__":
        
       if 
    not os.path.exists(sys.argv[1]) or not os.path.exists(sys.argv[2]):
           print 
    "Invalid file arguments given."
           
    print "Usage: renamer old_file_list new_file_list."
           
    sys.exit(1)
        
       
    old_names open(sys.argv[1], "r").readlines()
       
    new_names open(sys.argv[2], "r").readlines()
        
       for (
    oldnamenewnamein zip(old_namesnew_names):        
          
    real_oldname os.path.abspath(oldname.strip())
          
    real_newname os.path.abspath(newname.strip())  
          
    os.rename(real_oldnamereal_newname

  10. #10
    Join Date
    Nov 2009
    Beans
    187
    Distro
    Kubuntu 11.10 Oneiric Ocelot

    Re: Python Script For Batch Renaming

    Excellent, thanks for all the help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •