4

When I am running a script (it checks to which database a mxd is connected) I get the following error:

return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2265' in position##: character maps to <undefined>

Script:

import os, arcpy

directory = r"C:\Project"
for root, dirs, files in os.walk(directory):
for myFile in files:
        fileExt = os.path.splitext(myFile)[1]
        if (fileExt == ".mxd"):
            fullPath = os.path.join(root, myFile)
            print myFile

            myMap = arcpy.mapping.MapDocument(fullPath)
            layers = arcpy.mapping.ListLayers(myMap)

            for layer in layers:
                wsPath = ""
                try:
                    wsPath = layer.workspacePath
                except:
                    pass
                if wsPath:
                    print "    " + layer.name + " : " + wsPath

Is there a way to keep the script running and catch an exception?

7

This may get you closer to what you're looking for. i'm not sure if you're getting error on getting the layer or the map info, but this should give you both distinctly

from __future__ import print_function
import codecs
import os
import arcpy

with codecs.open(os.path.join(os.getcwd(), 'logfile.txt'),
                'w',
                encoding="utf-8") as f:

    directory = r"C:\Project"
    for root, dirs, files in os.walk(directory):
        for myFile in files:
            fileExt = os.path.splitext(myFile)[1]
            if (fileExt == ".mxd"):
                try:
                    print(u'\nWorking on mxd: ' + myFile, file=f)
                    fullPath = os.path.join(root, myFile)

                    myMap = arcpy.mapping.MapDocument(fullPath)

                    for layer in arcpy.mapping.ListLayers(myMap):

                        try:
                            wsPath = layer.workspacePath
                            print(u' layer name: {}, layer path: {}'.format(layer,
                                                                            wsPath),
                                                                            file=f)
                        except Exception as e:
                            print(u' layer name: {}, ERROR: {}'.format(layer,
                                                                       e.message),
                                                                       file=f)


                except Exception as e:
                    print(u'Problem with mxd', file=f)
                    print(e.message, file=f)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.