469,292 Members | 1,293 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

TypeError: cannot concatenate 'str' and 'NoneType' objects

Hi.

I'm extremely new to Python and programming as a whole. I have written
a python script with the assistance of ESRI ArcGIS 9.2, which uses
Python 2.4.1, however, it gives me this error when I try to run it.
I've already posted at ESRI support, and I was hoping that Python
people could help me more.

I hope there is something simple I could do to be able to define the
object that it thinks is NoneType. Please when someone responds,
please treat me as an absolute novice.

Thank you,
Marisa

Here is my code:

#
---------------------------------------------------------------------------
# towntab92.py
# Created on: Wed Dec 20 2006 11:09:59 AM
# (generated by ArcGIS/ModelBuilder)
# Created by Marisa Thompson
#
---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial
Analyst Tools.tbx")

# Define workspace
gp.workspace = "F:/Marisa/inputfolder"

# Define Variables
raster = "F:/Marisa/outputfolder_72/mss-72-spf.img"

#Get list of Town Shapefiles
Townshp = gp.ListFeatureClasses ("*")

#Store path to output folder
outputPath = "F:/Marisa/outputfolder_72"

# Begin going through the loop
Townshp = Townshps.next()
while Townshps !="":
#Set the output name to be the same as input
outName = outputPath + "/" + Townshp + "land" + ".img"
Output_table = outputPath + "/" + Townshp + "table" + ".dbf"
#For each extract by Mask
gp.ExtractbyMask_sa (raster, Townshp, outName)
#For each tabluate area
gp.TabulateArea_sa (Townshp, "RITOWN5K_", outName, "VALUE",
Output_table, "98.425")
Townshp = Townshps.next()

Dec 20 '06 #1
9 23515
th*************@gmail.com wrote:
I'm extremely new to Python and programming as a whole. I have written
a python script with the assistance of ESRI ArcGIS 9.2, which uses
Python 2.4.1, however, it gives me this error when I try to run it.
I've already posted at ESRI support, and I was hoping that Python
people could help me more.

I hope there is something simple I could do to be able to define the
object that it thinks is NoneType.
that would be the None object.

http://effbot.org/pyref/None

which is often used as a placeholder in Python.

my guess is that it's the next() call that returns None when you've
reached the end of the shapefile list; try changing

while Townshps !="":

to

while Townshps is not None:

and see if the problem goes away.

if you still get an exception, please post the full traceback; see

http://effbot.org/pyfaq/tutor-i-need...at-should-i-do

for details.

</F>

Dec 20 '06 #2
# Begin going through the loop
Townshp = Townshps.next()
while Townshps !="":
#Set the output name to be the same as input
outName = outputPath + "/" + Townshp + "land" + ".img"
Output_table = outputPath + "/" + Townshp + "table" + ".dbf"
#For each extract by Mask
gp.ExtractbyMask_sa (raster, Townshp, outName)
#For each tabluate area
gp.TabulateArea_sa (Townshp, "RITOWN5K_", outName, "VALUE",
Output_table, "98.425")
Townshp = Townshps.next()
Warning: I know nothing about the Python ArcGIS stuff.

The first thing that jumps out at me is your while condition. You are
testing "Townshps" when it seems from the code that you should be
testing "Townshp". However, the typical Python way to iterate through
a list for be to use a for loop.
Perhaps replace the while statement with:
for Townshp in Townshps:
and remove the "Townshp = Townshps.next()" lines

If that doesn't do it, please post the entire traceback message that
you are seeing (copy and paste it) and it should tell us a lot more
about your error.

Dec 20 '06 #3
On Wed, 2006-12-20 at 20:22 +0100, Fredrik Lundh wrote:
th*************@gmail.com wrote:
I'm extremely new to Python and programming as a whole. I have written
a python script with the assistance of ESRI ArcGIS 9.2, which uses
Python 2.4.1, however, it gives me this error when I try to run it.
I've already posted at ESRI support, and I was hoping that Python
people could help me more.

I hope there is something simple I could do to be able to define the
object that it thinks is NoneType.

that would be the None object.

http://effbot.org/pyref/None

which is often used as a placeholder in Python.

my guess is that it's the next() call that returns None when you've
reached the end of the shapefile list; try changing

while Townshps !="":

to

while Townshps is not None:

and see if the problem goes away.
Actually those will both lead to an infinite loop, since you're
comparing the entire list, which is unlikely to ever become "" or None.
You should compare Townshp, not Townshps, to "" or None, whichever
the .next() method actually returns to signal the end of the list.

Then again, I have the sneaking suspicion that you didn't post the
actual code that you're running. The code you posted looks like it
should raise a NameError in the line that says "Townshp =
Townshps.next()".

-Carsten
Dec 20 '06 #4
Fredrik Lundh wrote:
while Townshps is not None:
or rather,

while Townshp is not None:

since that's the variable you're using later on.

</F>

Dec 20 '06 #5
Mark Peters wrote:
However, the typical Python way to iterate through a list for be to
use a for loop. Perhaps replace the while statement with:
for Townshp in Townshps:
and remove the "Townshp = Townshps.next()" lines
that assumes that the feature class list is actually a Python iterable,
of course, and not just something that happens to have a "next" method.

:::

.... and judging from the documentation

http://webhelp.esri.com/arcgisdeskto...asses%20method

it's not an iterable.

:::

you could of course replace the

fcs = gp.ListFeatureClasses()
fc = fcs.next()
while fc:
...
fc = fcs.next()

pattern with

for fc in iter(gp.ListFeatureClasses().next, None):
...

but maybe that's a bit too clever for an absolute novice ;-)

</F>

Dec 20 '06 #6
th*************@gmail.com a écrit :
Hi.

I'm extremely new to Python and programming as a whole. I have written
a python script with the assistance of ESRI ArcGIS 9.2, which uses
Python 2.4.1, however, it gives me this error when I try to run it.
Please post the full traceback. It's meant to help finding out what went
wrong, you know...
I've already posted at ESRI support, and I was hoping that Python
people could help me more.

I hope there is something simple I could do to be able to define the
object that it thinks is NoneType.
That's certainly not the solution...
Please when someone responds,
please treat me as an absolute novice.

Thank you,
Marisa

Here is my code:
(snip)
#Get list of Town Shapefiles
Townshp = gp.ListFeatureClasses ("*")
(snip)
>
Townshp = Townshps.next()
while Townshps !="":
1/ where does this "Townshps" comes from ?
2/ naming conventions are to use either all_lower (preferred) or at
least mixedCase names for variables. And by all mean be consistent.
3/ if Townshp is a shortcut for Township, then it would be better to
keep the full word
4/ the Python 'for' loop is meant to iterate over an iterable and taking
care of boundaries, so you'd be better using it:

townships = gp.ListFeatureClasses ("*")
for township in townships:
doSomethingWith(township)
Dec 20 '06 #7
On Wed, 2006-12-20 at 11:33 -0800, Mark Peters wrote:
Warning: I know nothing about the Python ArcGIS stuff.

The first thing that jumps out at me is your while condition. You are
testing "Townshps" when it seems from the code that you should be
testing "Townshp". However, the typical Python way to iterate through
a list for be to use a for loop.
Perhaps replace the while statement with:
for Townshp in Townshps:
I was considering suggesting this, but I'm not sure there is much hope
that this would work. The posted code suggests that Townshps.next()
returns "" or None when the end of the list is reached. In order for the
for loop to work, it would have to raise StopIteration instead. (I
suppose it is conceivable that iter(Townshps) would return an object
that does behave in the required manner, but I wouldn't bet a lot of
money on that.)

Of the course if Townshps does not support the iteration protocol, the
for loop could still be written using the iter(callable,sentinel)
pattern, but let's not confuse this poor newbie more than necessary ;)

-Carsten
Dec 20 '06 #8
Bruno Desthuilliers a écrit :
th*************@gmail.com a écrit :

4/ the Python 'for' loop is meant to iterate over an iterable and taking
care of boundaries, so you'd be better using it:

townships = gp.ListFeatureClasses ("*")
for township in townships:
doSomethingWith(township)
Actually, forget the for loop (cf Fredrik's post - looks like
gp.ListFeatureClasses() doesn't return an iterable...)
Dec 20 '06 #9
I've had a lot of replies suggesting different things. These are the
results:

When I change

while Townshps !="": to
while Townshp is not None:

and when I change

while Townshps !="": to
for Townshp in iter(gp.ListFeatureClasses().next, None):

They both work perfectly, and it solved my problem.

Thank you all so much.
Marisa
On Dec 20, 2:22 pm, Fredrik Lundh <fred...@pythonware.comwrote:
thompson.mar...@gmail.com wrote:
I'm extremely new to Python and programming as a whole. I have written
a python script with the assistance of ESRI ArcGIS 9.2, which uses
Python 2.4.1, however, it gives me this error when I try to run it.
I've already posted at ESRI support, and I was hoping that Python
people could help me more.
I hope there is something simple I could do to be able to define the
object that it thinks is NoneType.that would be the None object.

http://effbot.org/pyref/None

which is often used as a placeholder in Python.

my guess is that it's the next() call that returns None when you've
reached the end of the shapefile list; try changing

while Townshps !="":

to

while Townshps is not None:

and see if the problem goes away.

if you still get an exception, please post the full traceback; see

http://effbot.org/pyfaq/tutor-i-need...-error-in-my-p...

for details.

</F>
Dec 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
6 posts views Thread by Mike Brown | last post: by
3 posts views Thread by Ernesto | last post: by
9 posts views Thread by k.retheesh | last post: by
2 posts views Thread by AWasilenko | last post: by
6 posts views Thread by ssecorp | last post: by
10 posts views Thread by est | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.