Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

When I`m 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?

Thanks,

Trashken

share|improve this question
1  
    
Could you please upload the script ? –  Sunil Feb 26 '13 at 9:55
add comment

1 Answer

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)
share|improve this answer
    
Thanks. Worked great for me. –  Trashken Feb 28 '13 at 8:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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