os.path.exists discrepancy

Robert Brewer fumanchu at amor.org
Mon Nov 15 08:47:18 CET 2004

I've got a webapp which is behaving oddly. I want to include an image on
the webpage, but only if the physical file exists; if it doesn't, I
don't want an img element to generate an ugly blank box. I know the
physical location of the image(s) in question, and figured I'd use
os.path.exists to see if each file exists. A quick test in the
interpreter yields:

>>> os.path.exists(u'S:/Scans/Projects/2004/TJ/04TJ0267.jpg')

However, when I run the following code in the webapp (served by Apache2
+ mod_python on the same Win2k machine):

    if not os.path.exists(path):
        scan = u"<!-- scan image not found: %s -->" % repr(path)

...I get the output:

<!-- scan image not found: u'S:/Scans/Projects/2004/TJ/04TJ0267.jpg' -->

As you can see, the two paths are equal. Prompting my reaction: bzuh? My
next thought was to read through the os module source--perhaps the
import of "path" was dependent on context. It is, but os.path.__file__
is the same in both contexts: 'C:\\Python23\\lib\\ntpath.pyc'

Any ideas or workarounds?

Robert Brewer
Amor Ministries
fumanchu at amor.org

More information about the Python-list mailing list