ArcGIS 10:Doing Python to check for numbers of files in a directory-indentation err

3448
13
08-17-2012 10:53 AM
ScottChang
Deactivated User
Hi all
I am using the following tutorial materials to pick up the Python programming:

(i)                  Python Programming for ArcGIS by David Quinn & Daniel Sheehan (dated: January 27, 2011)

(ii)                Python �?? Check for number of shapefiles in a directory in http://gis.stackexchange.com/questions/30059

I tried the following ArcPy code in my ArcMap 10:
 >>> import arcpy
>>> import os
>>> import glob 
>>> # Check numbers of shapefiles in a directory
>>> path = "C:\\TEMP\\"
>>> first = glob.glob(os.path.join(path,"NavyAnnexPt06move*"))
>>> first 
['C:\\TEMP\\NavyAnnexPt06move.dbf', 'C:\\TEMP\\NavyAnnexPt06move.prj', 'C:\\TEMP\\NavyAnnexPt06move.sbn', 'C:\\TEMP\\NavyAnnexPt06move.sbx', 'C:\\TEMP\\NavyAnnexPt06move.shp', 'C:\\TEMP\\NavyAnnexPt06move.shx']
>>> # Also to check for the only filename in path
 
>>> def file_check(path):
...      """Returns true if in path there is only one basename"""
... import os
... mlist = []I am using the following tutorial materials to pick up the Python programming:
...       for file in os.listdir(path):
...           if os.path.basename(file).split(".")[0] not in mlist:
...              mlist.append(os.path.basename(file).split(".")[0])
...     if len(mlist)>1:
...              return True
...       else:
...              return False
... 

Parsing error <type 'exceptions.IndentationError'>: unexpected indent (line 5)
>>> 


and I got an indentation error. Please help, advise and respond.

Thanks,
Scott Chang
Tags (2)
0 Kudos
13 Replies
ScottChang
Deactivated User
Hi Thomas, Thanks for your nice, valuable response.
I am new in doing the Python-ArcPy programming. I will be very careful in picking up the pecurious skills of Python syntax.
I corrected my old arcpy script and executed it.  It worked nicely and I got the output.
I tried your arcpy script for the Circle class:
>>> import arcpy
>>> import os
>>> import glob 
>>> class Circle:
...     def __init__(self):
...         self.radius = 1
... my_circle = Circle()
... print (2 * 3.14 * my_circle.radius)
... my_circle.radius = 5
... print (2 * 3.14 * my_circle.radius)
... 
6.28

31.4

>>> class Circle:
...     def __init__(self, radius = 1):
...         self.radius = radius
... my_circle = Circle() #has a radius of 1 as no explicit value was given
... my_circle_2 = Circle(2) # has a radius of 2
... 
>>> 


I think it worked too.  But I did not see the output of the 2 circles. What should I do to get the output of the 2 circles?
Please kindly help, advise and respond again.

Many Thanks,
Scott Chang
0 Kudos
ThomasEmge
Esri Contributor
Scott,

my second approach with

def __init__(self, radius = 1):


is a short hand notation that allows the user to set the radius at the time of the object creation.

radius = 1


means that when you create the object you can specify a value for the python attribute called radius. If you don't provide an explicit value then the __init__ method assumes that the radius has a value of 1.
Essentially method 1 and method 2 yield the same result:

# method 1
my_circle = Circle()
my_circle.radius = 2
# method 2
my_circle = Circle(2)


If this notation is rather confusing you then don't worry about.

As a recommendation I would suggest to stay away from classes for the moment and to revisit the concept in a couple weeks when you are feeling more at home in Python.

- Thomas
0 Kudos
ScottChang
Deactivated User
Hi Thomas,
I did few trials to print the output of your arcpy script:
>>> my_circle
<__main__.Circle instance at 0x302221E8>
>>> my_circle_2
<__main__.Circle instance at 0x30222148>
>>> print my_circle
<__main__.Circle instance at 0x302221E8>

>>> return my_circle
Runtime error <type 'exceptions.SyntaxError'>: 'return' outside function (<string>, line 1)
>>> return 'my_circle'
Runtime error <type 'exceptions.SyntaxError'>: 'return' outside function (<string>, line 1)
>>> print (my_circle)
<__main__.Circle instance at 0x302221E8>

>>> print (Circle())
<__main__.Circle instance at 0x30222120>

>>> print (2 * 3.14 * my_circle)
Runtime error <type 'exceptions.TypeError'>: unsupported operand type(s) for *: 'float' and 'instance'
>>> print (2 * 3.14 * my_circle_2)
Runtime error <type 'exceptions.TypeError'>: unsupported operand type(s) for *: 'float' and 'instance'


I cannot get the decent output.  Please tell me how I can get the output of your methods.

Thanks,
Scott Chang
0 Kudos
ThomasEmge
Esri Contributor
I see.

Take a look at the first line you have typed.

>>> my_circle
<__main__.Circle instance at 0x302221E8>


What this line is telling that 'my_circle' is an instance of the Circle class living at address location 0x302221E8. That is it - nothing more or less. The notion of a class is following the principles of object-oriented programming and again is not really something I would recommend to a beginner. You can google/bing/yahoo "object-oriented principles" but the findings are probably more confusing than helpful right now.

How does the Python line help you right now? Not at all.....

You want to find out what this 'my_circle' thing really is and the Python way to do that is

>>> dir(my_circle)
['__doc__', '__init__', '__module__', 'radius']


The Python dir() function will tell you more an object, its attributes and methods.
For 'my_circle' you can see that there built-in functions like __doc__, __init__, __module__ and you also find the class attribute radius. In order to find the radius of the my_circle objects you would write

>>> print my_circle.radius
1


Again my recommendation would be: don't use the class syntax -- it is over-engineering the problem. Keep it simple and use variables as of now such that:

myCircleRadius = 2
myCircleCircumference = 2 * 3.14 * myCircleRadius



- Thomas
0 Kudos