Advertisement

07.18.2008 at 12:56PM PDT, ID: 23578050
[x]
Attachment Details

Python - Number of characters stripped from folder name

Asked by mgjust in Python Scripting Language

Hello,
I am using this code from :

http://www.experts-exchange.com/Q_23068228.html

In the past my files names/folder had only two digits at the end of the name. But now they have three (i.e. 001 - 448)

can i just change lines

36: numSuffix = LnumDir[-2:]   # last 2 chars
37: idSuffix = LnumDir[-4:]    # last 4 chars

to


36: numSuffix = LnumDir[-3:]   # last 3 chars
37: idSuffix = LnumDir[-5:]    # last 5 chars


Also, if they are missing numbers (i.e. 001 - 300, 310 - 400, 407, 409, 448) in the landcover folders will this still execute properly?

Thanks,
MJ

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
# Multi_Output_Map_Algebra_sample.py
# Description:
#   Runs an expression built with the Map Algebra language.
# Requirements: None
 
import arcgisscripting
import glob
import os
 
gisworkDir = 'c:\Giswork'
 
def log(s):
    f = open("C:/tester.txt","w")
    f.write(s)
    f.close()
 
 
def allLandcoverDonutAndIdSuffix(gisworkDir):
    '''Generates all tuples (landcover, donut, idSuffix) from the gisworkDir.'''
    ##log("1\n")
    # Prepare the path to the donuts.
    donutDir = os.path.join(gisworkDir, 'landcoverprep', 'Donut')
    assert os.path.isdir(donutDir)
    ##log("2\n")
    # Iterate through sequence of top-level landcover subdirectories.
    mask = os.path.normpath(os.path.join(gisworkDir, 'Landcoverprep\landcover*'))
    ##log(mask)             
    for LDir in glob.glob(mask):      # full paths to LandcoverA, LandcoverB, etc.
        subdir = os.path.basename(LDir)
        ##log("3\n")
        # Iterate through all numbered landcover subdirs inside.
        mask2 = os.path.join(LDir, '*')
        for LnumDir in glob.glob(mask2):
            # Separate the counter part, the idSuffix and construct 
            # the related donut numbered directory name.
            numSuffix = LnumDir[-2:]   # last 2 chars
            idSuffix = LnumDir[-4:]    # last 4 chars
            DnumDir = os.path.normpath(
                         os.path.join(donutDir, 'DONUT_' + numSuffix))
            assert os.path.isdir(DnumDir)  
            ##log("4")
            # Generate another output tuple.
            yield (LnumDir, DnumDir, idSuffix)
 
 
# Create the Geoprocessor object
gp = arcgisscripting.create()
expTemplate = 'CON(ISNULL(%s), %s, %s * -1)'
log("\n===== Output below: ====\n")
 
for landcover, donut, idSuffix in allLandcoverDonutAndIdSuffix(gisworkDir):
    # Construct the expression and the output directory name.
    outDir = os.path.join(gisworkDir, 'landcoverprep', 'Results', idSuffix)
    log("\n" + outDir)
    InExpression = expTemplate % (donut, landcover, landcover)
    log("  " + InExpression)
 
    # Check out Spatial Analyst extension license
    gp.CheckOutExtension("Spatial")
 
    # Process: MapAlgebra
    gp.SingleOutputMapAlgebra_sa(InExpression, outDir)
 
    log("Msg: " + gp.GetMessages())
[+][-]07.18.2008 at 01:05PM PDT, ID: 22039317

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.19.2008 at 03:57AM PDT, ID: 22042154

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Sign Up Now!
Solution Provided By: pepr
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.19.2008 at 10:53AM PDT, ID: 22043207

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.20.2008 at 08:30PM PDT, ID: 22047894

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.20.2008 at 08:30PM PDT, ID: 22047896

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628