#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    17
    Rep Power
    0

    Exclamation Renaming files using os.rename()


    Please see http://forums.devshed.com/t269923/s.html for the background information that brought me here.

    My dilema is simple. I have one textfile with the names of all the files in a directory. I have a second textfile with the names the files in that same directory are supposed to be. Each textfile is sorted and organized so that line1 in file1 = line 1 in file 2. I need to go down the lists and rename all the files in the directory to the names in list 2.

    I was given this sample code to use but I'm getting some errors when I try to run the python script. Here's the code.

    Code:
    import os
    oldNames = open('oldname.txt')
    newNames = open('newname.txt')
    for (old ,new) in zip(oldNames, newNames):
      os.rename(old ,new)
    Here is the error that I get when I attempt to run it in IDLE.

    Code:
    Traceback (most recent call last):
      File "C:\Python24\Tools\Scripts\New Folder\rename.py", line 6, in ?
        os.rename(old ,new)
    OSError: [Errno 22] Invalid argument
    This is my very first attempt coding in Python and everything I know I've learned in the first hours of this morning. I suspect from reading the help file that it's not processing the os.rename() function. I tried using the os.rename() function to do a single file.

    Code:
    os.rename('1327.jpg',file1.jpg)
    This worked and renamed the file. However I can't use this for each file individually. I need to use the names in the two textfiles because I have over 6200 files to rename. I've placed 6 picture files, the script, and the two text files in a directory to test it. The error above is the result. Can someone help me understand why I'm getting the error and help me fix it?

    Thanks in advance.

    EDIT:
    In case anyone asks, yes I have done a search here. I found 3 topics regarding the use of os.rename(). One was similar but the author was needing something more advanced than what I need. I downloaded the script used in that example but wasn't able to modify it to help any.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    rename() is a very simple function but I can see how it might be confusing at first . What you need to do is pass the old name of the file you want to rename (including the full path) and the new name, something like this.

    Code:
    #!/usr/bin/env python
    
    from os import rename
    
    oldNames = [fileName.strip() for fileName in file('old.txt')]
    newNames = [fileName.strip() for fileName in file('new.txt')]
    
    for (old, new) in zip(oldNames, newNames):
        rename(old, new)
    Whats going on? This probably wont name much since yet but its fairly cool Python, I'll explain.

    Firstly the rename function is imported from the os module. After this is done the oldNames and newNames are read into their respective lists after having any trailing whitespace (new line characters) removed by means of a list comprehension a for loop that returns a list.

    After this is done all that we have left to do is to rename the files. since these are held in different lists we're using the zip() function here to pass the old and new name to rename(). Done, if all goes well the files will be renamed .

    Note: This code hasn't been tested, let me know if you have any problems with it and I'll be happy to fix it ASAP

    I hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Colorado, USA
    Posts
    68
    Rep Power
    10
    Your problem, is that this method leaves newlines in the file name string.
    Also, remember a good method to troubleshoot things like this is to simply print out your strings as you go to see if there is anything weird going on.

    A simple work around is

    Code:
    import os
    oldNames = open('oldname.txt')
    newNames = open('newname.txt')
    for (old ,new) in zip(oldNames, newNames):
         old = old.replace('\n','')
         new = new.replace('\n','')  
         os.rename(old ,new)
    I am sure there is some more graceful way to do this, but this method works.<edit> Netytan beat me to it by mere seconds</edit>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    17
    Rep Power
    0
    That makes sense. That was very well explained as to how that function handles the text file information. I copied and pasted your code and gave it a shot and it worked, after some minor changes to the text filenames.

    Thank you very very much. Man...I've been banging my head against VBS for the last 2 weeks with about 20+ lines of code that python handled with 5.

    I think I'm going to do more messing around in Python.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by drtech
    Thank you very very much. Man...I've been banging my head against VBS for the last 2 weeks with about 20+ lines of code that python handled with 5.
    Python is very very great about that. That is why I love it so much. I can do very powerfull things with just a few lines.

IMN logo majestic logo threadwatch logo seochat tools logo