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.

I am trying to automate a naming process that takes the file name from a list of file paths to name other folders, geodatabases, zipfiles, etc.

The problem I'm running into is that, I assume, I am trying to run string functions on list object and I can't figure out how to get the script to do what I need it to do.

For example, I have the file path C:\TEMP\Durham.shp. I want to name a geodatabase, folder, and zipfile "Durham". Instead I get a folder and geodatabase named "C".

Can someone take a look at my script and tel me what I'm doing wrong?

The following script is that last incarnation of several attempts.

import arcpy, os
import zipfile
from arcpy import env
from os.path import basename

BASELAYERS = arcpy.GetParameterAsText(0)
CLIPLAYERS = arcpy.GetParameterAsText(1)
OUTFOLDERMASTER = arcpy.GetParameterAsText(2)
FILETYPES = ["*.shp", "*.dbf", "*.shx", "*.prj", "*.sbn", "*.sbx", "*.xml"]

env.workspace = OUTFOLDERMASTER
for LAYERS in CLIPLAYERS:
    LAYERSSTR = ''.join(LAYERS)
    BASENAME = LAYERSSTR.split("\\")[-1]
    FOLDER = os.path.join(OUTFOLDERMASTER, BASENAME)
    os.makedirs(FOLDER)
    PGDBNAME = "NCFlood_Effective_" + BASENAME + "_PGDB.mdb"
    arcpy.CreatePersonalGDB_management(FOLDER, PGDBNAME, "9.3")
    PGDB = os.path.join(FOLDER,PGDBNAME)
    for FILES in BASELAYERS:
        FEATURENAMESTR = ''.join(FILES)
        FEATURENAME = os.path.basename(FEATURENAMESTR)
        SHAPEFILENAME = FEATURENAME + ".shp"
        SHAPEFILE = os.path.join(FOLDER, SHAPEFILENAME)
        arcpy.Clip_analysis(FILES, LAYERS, FEATURENAME)
        arcpy.Clip_analysis(FILES, LAYERS, SHAPEFILE)
    ZIPFILELIST = []        
    for TYPE in FILETYPES:
        ZIPFILELIST.extend(glob.glob(FOLDER))
    ZIPFILENAME = os.path.join(FOLDER, "NCFlood_Effective_" + FOLDERNAME + "_SHP.zip")
    ARCHIVE = zipfile.ZipFile(ZIPFILENAME, "w")
    for ZIP in ZIPFILELIST:
        ARCHIVE.write(ZIP, os.path.basename(ZIP), zipfile.ZIP_DEFLATED)
    ARCHIVE.close()

I have also tried

    BASENAME = os.path.basename(LAYERS)
    FOLDERNAME = os.path.splitext(BASENAME)[0]
share|improve this question

1 Answer 1

up vote 4 down vote accepted

I think your problem is that in its current state CLIPLAYERS is a string and not a list. This means that when you loop through it using for LAYERS in CLIPLAYERS it is going to give you one character at a time starting with C.

You can avoid this by adding CLIPLAYERS = CLIPLAYERS.split(";") before you loop through CLIPLAYERS. This will create a list from a semicolon delimited string of layers.

share|improve this answer
    
That work. Thanks. –  NCFMP Jan 18 '13 at 16:44
    
@NCFMP - Don't forget to mark as answered, please. –  Chad Cooper Jan 18 '13 at 16:49

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.