Why am I getting this error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Longest

    Why am I getting this error?

    I am attempting to write a script that will take a directory as an argument then go through said directory and rename files with a given extension (in this case .jpg)

    here is what I have so far:
    Code:
    import os
    fileDir=os.listdir("c:\\Users\\Nathan\\Desktop\\Cell Pics\\")
    count = 1
    for fn in fileDir:
       
        if fn[-4:] == '.jpg':
            os.rename(fn,'%03i.jpg' % count)
            count += 1
    now, when I replace the directory above with '.' and drop the script into the directory I want to affect it will do its job perfectly. However when I have the directory put into the parameter of os.listdir and attempt to run it I get the following error:
    Code:
    Traceback (most recent call last):
      File "C:/Python27/test2.py", line 7, in <module>
        os.rename(fn,'%03i' % count)
    WindowsError: [Error 2] The system cannot find the file specified
    What am I doing wrong?

    I am using windows atm but this script is for UNIX. (if that matters let me know) I am pretty new to scripting and python (first week ever programming) so please be descriptive if at all possible!

    thanks in advance!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Join the path and the file name.
    Code:
    os.path.join("c:\\Users\\Nathan\\Desktop\\Cell Pics\\", fn)

    Comment

    Working...