1

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]

1 Answer 1

4

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.

1
  • @NCFMP - Don't forget to mark as answered, please. Commented Jan 18, 2013 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.