#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    67
    Rep Power
    11

    Exclamation TypeError: unsubscriptable object?


    I have this bit of code:
    Code:
    def convert(self, charFrom, charTo, dir):
            if os.path.exists(dir) == True:
                finalDir = dir
                fileList = os.listdir(finalDir)
                os.chdir(finalDir)
                for i in fileList:
                    splitFileName = os.path.splitext(i)
                    renamedFileNameP1 = splitFileName[0].replace(charFrom, charTo, 10000)
                    print renamedFileNameP1
                    print splitFileName[1]
                    fileName = ''.join[(renamedFileNameP1, splitFileName[1])]
                    os.rename(i, fileName)
                return "Done"                             
            else:
                return "Error! The directory: ", dir, " is invalid."
    which is supposed to, for all the file in directory dir, replace
    all instances of charFrom with charTO. However, I get the error mentioned in the thread title. Does anyone know why?
    Thanks.
    Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    You didn't supply the traceback so I'm guessing that elsewhere in your program you are trying to call your function with convert[] instead of convert().

    Grim
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    67
    Rep Power
    11
    No, I don't do that.
    Here's the traceback:
    Code:
    Traceback (most recent call last):
      File "C:\Documents and Settings\Evan\My Documents\Programming\Programs\Python\fileutility.py", line 77, in -toplevel-
        start.cmdloop()
      File "C:\Python23\lib\cmd.py", line 133, in cmdloop
        stop = self.onecmd(line)
      File "C:\Python23\lib\cmd.py", line 210, in onecmd
        return func(arg)
      File "C:\Documents and Settings\Evan\My Documents\Programming\Programs\Python\fileutility.py", line 22, in do_convertUnderscoreToSpace
        done = self.convert("_", " ", dir)
      File "C:\Documents and Settings\Evan\My Documents\Programming\Programs\Python\fileutility.py", line 69, in convert
        fileName = ''.join[(renamedFileNameP1, splitFileName[1])]
    TypeError: unsubscriptable object
    Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    67
    Rep Power
    11
    I figured it out.
    Thanks for the help.
    Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    I knew it was something like that, happens to me all the time.
    Grim

IMN logo majestic logo threadwatch logo seochat tools logo