Why is abspath returning inconsistent file paths from same directory?

135 views Asked by At

I'm using os.walk() to traverse a folder and get the fully-qualified path for certain documents for processing.

def folderLoop():
    for path, dirs, files in os.walk(inFolder):
        for filename in files:
            if fnmatch.fnmatch(filename, '*.mxd'):
                #mxdFilePath = os.path.abspath(os.path.join(path, filename))
                mxdFilePath1 = os.path.normpath(os.path.join(path, filename))
                mxdFilePath = os.path.join(os.getcwd(), mxdFilePath1)
                print("\tRe-source: " + mxdFilePath)
                #mxd = arcpy.mapping.MapDocument(r'{}'.format(mxdFilePath))
                ### Process File ###

I read this post and you can see I've tried more than one method to get the correct path, but for some reason I haven't yet discovered, the result path seems to change depending on whether or not the last line is commented or not.

If I run the code with the mxd assignment commented out as shown above, all of the file paths printed look correct:

Re-source: C:\Users\CoryDavis\desktop\test_data\IL\Bond_IL\bond_il.mxd
Re-source: C:\Users\CoryDavis\desktop\test_data\IL\Bond_IL\bond_il_cmp.mxd
Re-source: C:\Users\CoryDavis\desktop\test_data\IL\Bond_IL\bond_il_detailed_outline.mxd
...

However, this is what I see if I uncomment it:

Re-source: C:\Users\CoryDavis\desktop\test_data\IL\Bond_IL\bond_il.mxd
Re-source: C:\Users\CoryDavis\desktop\test_data\IL\Bond_IL\test_data\IL\Bond_IL\bond_il_cmp.mxd

Then the assignment fails because the path to the second file is incorrect. Why does the second path repeat the relative path? Why does the printed string change if I uncomment the assignment statement? Why does the first file path remain unaffected?

0

There are 0 answers