External Module causes os.path.isfile to return incorrect answer
Question: I am running Python 2.7 under Windows7 on an iMAC (using BOOTCAMP) and have
encountered a strange problem or bug. Here is my problem script boiled down to
essentials:

    
    
    import arcpy
    import arcpy.mapping as mapping
    import os
    
    for fileName in os.listdir("TestDir"):
        fullPath = os.path.join("TestDir", fileName)
        #hexpath=':'.join(x.encode('hex') for x in fullPath)
        #print hexpath
        if os.path.isfile(fullPath):
            print fullPath + " is a file"
            desc = arcpy.Describe(fullPath)
            if(desc.datatype == "MapDocument"):
                #mxd = mapping.MapDocument(fullPath)
                #del mxd
                print "MAP FILE: " + fileName
        else:
            print fullPath + " is not a file"
    

When the line "mxd=" is commented I get the correct output

    
    
    TestDir\A_layer.lyr is a file
    TestDir\Mapfile1.mxd is a file
    MAP FILE: Mapfile1.mxd
    TestDir\Z_file is a file
    

When the line is uncommented I get:

    
    
    TestDir\A_layer.lyr is a file
    TestDir\Mapfile1.mxd is a file
    MAP FILE: Mapfile1.mxd
    TestDir\Z_file is not a file
    

The last line printed differs and is WRONG. (I get the correct behavior again
if I uncomment the "del mxd" line).

Without getting into details about what the external module does, or simply
dismissing the module as buggy, my question is:

**What possible bug or type of bug in the mapping.MapDocument method could
possibly lead to such a behavior? How can a bug in a user written module
affect so profoundly the output of a built-in library?**

Unfortunately the source of the arcpy module is not available to me. It is
vendor proprietary. The vendor asserts that in their testing the bug cannot be
reproduced with very similar test case on the same platform and Python
version.


Answer: The problem is apparently that the `mapping.MapDocument` constructor changes
the current working directory. This means that you're not looking for
`TestDir\Z_File` under the original working directory, you're looking for it
under a different directory, and there is no such file, and `isfile` returns
False for no such file, not just for being a directory or other non-file
thing.

The best workaround is probably to
[`abspath`](https://docs.python.org/2/library/os.path.html#os.path.abspath)
all of your paths before ever constructing a `MapDocument`.

