Select to view content in your preferred language

Parsing list object file paths for file name

2858
3
Jump to solution
01-18-2013 06:18 AM
JohnLay
Occasional Contributor
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]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Well you could use simple variations of the split python functions:  'rsplit', 'split', or, say 'splitext'

So using the string you provided, you want to extract the text 'Durham':
C:\TEMP\Durham.shp


Then, could do it as such:

import os
text = r'C:\TEMP\Durham.shp'
text = text.rsplit(os.sep, 1)[1]
text = os.path.splitext(text)[0]

# or in a single line:
text = os.path.splitext(text.rsplit(os.sep, 1)[1])[0]


EDIT:
Then, for 'assembling' new pathnames, you may find this useful:

newText = r'\\someServer\aFolder\someGDB.gdb'
newFCpath = os.path.join(newText, text)


...which results in the newFCpath string:
\\someServer\aFolder\someGDB.gdb\Durham


Hope that helps.
Wayne

View solution in original post

0 Kudos
3 Replies
T__WayneWhitley
Frequent Contributor
Well you could use simple variations of the split python functions:  'rsplit', 'split', or, say 'splitext'

So using the string you provided, you want to extract the text 'Durham':
C:\TEMP\Durham.shp


Then, could do it as such:

import os
text = r'C:\TEMP\Durham.shp'
text = text.rsplit(os.sep, 1)[1]
text = os.path.splitext(text)[0]

# or in a single line:
text = os.path.splitext(text.rsplit(os.sep, 1)[1])[0]


EDIT:
Then, for 'assembling' new pathnames, you may find this useful:

newText = r'\\someServer\aFolder\someGDB.gdb'
newFCpath = os.path.join(newText, text)


...which results in the newFCpath string:
\\someServer\aFolder\someGDB.gdb\Durham


Hope that helps.
Wayne
0 Kudos
PhilMorefield
Occasional Contributor III
Python is pretty good about recognizing and handling path names. Here's how I handle tasks like yours:
>>>from os import path
>>>text = r'C:\TEMP\Durham.shp'
>>>path.dirname(text)
'C:\\TEMP'
>>>path.basename(text)
'Durham.shp'
>>>newfolderName = path.basename(text)[-4:]
>>>newfolderName
'Durham'
>>>newPath = path.join('C:\\newLocation', newfolderName)
>>>newPath
'C:\\newLocation\\Durham'
0 Kudos
JohnLay
Occasional Contributor
Thanks guys! I found a solution to my issue, but I'm going to have to rewrite the script to handle SDE database pathnames, so I will give these options a shot.
0 Kudos