tags:

views:

88

answers:

2

Hi All,

I'm trying to rename some files, but getting a baffling error*. When I run this:

if os.path.isfile(fullPath):
    print 'fmf exists'
    print fullPath
    print newFilePath
    os.rename(fullPath,newFilePath)

I get the following error:

fmf exists
(correct fullPath)
(correct newFilePath, ie. destination)
Traceback (most recent call last):
  File "whatever.py", line 374, in ?
    os.rename(fullPath,newFilePath)
OSError: [Errno 2] No such file or directory

Since I know that the file at fullPath exists, I'm baffled by the error. Of course, newFilePath doesn't exist, because that would be dumb. Any hints?

Thanks! Alex

*Aren't they all?

+1  A: 

You can still get the exception if you try to copy to a directory that does not exist.

Ayman Hourieh
Perfect, thanks! I had confirmed that the newFilePath didn't exist (as indeed it shouldn't) but the dest directory was wrong as well.
Alex
+1  A: 

I can't see the full inner workings of your code, so here's my two cents worth:

Your newFilePath may contain a directory that doesn't exist. If that is the case, then depending on your operating system, your program is unable to create a file in a directory that doesn't exist. That could be your error.

Hope this helps

inspectorG4dget