1

os.path.exists is giving me incorrect answers.

it's not the same problem discussed at below link since I'm at windows. Are there other reasons for it to fail?

os.path.exists() lies

The test returns ok when I test it against a file at the same directory as the *.py script runs, but none of its sub directories..

-EDIT-

I'm using absolute path.

I'm looking at one of the sub directories as this script runs, and can literally see file's last modified time field being changed in the windows explorer.
There are no other stuff going on my computer I can think of that will modify the files in question.

def SaveIfNewer(doc, aiFile, pngFile):
    options = win32com.client.Dispatch('Illustrator.ExportOptionsPNG24')
    options.SetArtBoardClipping(True)
    if (os.path.exists(pngFile)):
        aiFileTime = os.stat(aiFile)[8]
        pngFileTime = os.stat(pngFile)[8]
        print("aiFileTime: ", aiFileTime, "pngFileTime: ", pngFileTime)

        if(aiFileTime > pngFileTime):
            os.remove(pngFile)

    if( not os.path.isfile(pngFile)):
        doc.Export(pngFile, constants.aiPNG24, options)
        print 'exporting:', pngFile
    else:
        print 'skipping file:', pngFile
Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • 7
    What are these incorrect answers? What makes your problem different from the problem in the link? Can you show us your code and a sample directory layout? – wkl Dec 30 '10 at 19:33
  • are you using absolute or relative paths? If relative, check the return from getcwd() – Paulo Scardine Dec 30 '10 at 20:17

2 Answers2

3

os.path.exists and os.path.isfile is not case sensitive in Windows machines.

Here's what I get in Windows 7 (Python 2.7)

>>> os.path.exists('C:/.rnd')
True
>>> os.path.exists('C:/.RND')
True
>>> os.path.isfile('C:/.rnd')
True
>>> os.path.isfile('C:/.RND')
True
Imran
  • 87,203
  • 23
  • 98
  • 131
1

Turned out, os.path.exists and os.path.isfile is case-sensitive..

Blah!

eugene
  • 39,839
  • 68
  • 255
  • 489