Projected datasets in one single line using a script tool

1891
5
10-14-2011 08:01 PM
AhmedAldossary
New Contributor
Hi,

I'm writing a script in python to re-project a set of shapefiles, and I want to report these re-projected shapefiles in a geoprocessing message in "a single line" seperated by commas with no trailing comma at the end, after processing the script tool. I used arcpy.AddMessage(), however, it reported the results in separate lines. I would like to mention that I used a loop here to list all features inside workspace i.e. ListFeatureClasses()

Does anyone have any idea of doing that?
Tags (2)
0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
I was able to do this by appending the feature classes to a list, writing the list to a text file, reading the text file as a string while removing the last comma, and then finally writing the string using the arcpy.AddMessage function.  Here is an example of how to do this with a list of feature classes:

import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

# Create an empty list
list = []

# Create a text file
output = open(r"C:\temp\python\list.txt", "w")

lstFCs = arcpy.ListFeatureClasses("*")
for fc in lstFCs:
    list.append(fc)

# write each feature class to the text file
for n in list:
    output.write(n + " ")

# read the text file and add commas
output = open(r"C:\temp\python\list.txt", "r")

s = output.read()
s = s.rstrip()
s = s.replace(" ", ", ")

arcpy.AddMessage(s)

# close and delete the text file
output.close()
os.remove(r"C:\temp\python\list.txt")
0 Kudos
AhmedAldossary
New Contributor
Thank you Jake for your help. You gave me an idea of to use a list to display the feature classes, although I took a different approach instead of writing feature classes to a text file. To explain more, after the loop, I convert the list to string using:
x = ", ".join(myList)
Then I used that in the geoprocessing tool as follows:
arcpy.AddMessage("Projected " + str(x))

Again, thank you for your help!
0 Kudos
EvanEchlin
New Contributor

Ahmed,

Thanks for the guidance here. I am looking to achieve the same goal with the script results separated by a comma. When I use the syntax;

myList = reprojectedFCs

x = ", ".join(myList)

arcpy.AddMessage("Projected " + str(x))

I end up with commas before and after every single letter - instead of after each feature class. I am new to scripting, any idea what I am doing wrong here?

Thanks,

Evan

0 Kudos
NeilAyres
MVP Alum

if myList contains only 1 object ie len(myList) == 1 then join, split etc indeed any list function will interate through the individual components.

So just check first if len(myList) > 1, before using ",".join(myList)

0 Kudos
JakeSkinner
Esri Esteemed Contributor
Awesome, your method will be much easier!  I was having trouble figuring out how to convert a list to a string.  Thanks for the syntax:

x = ", ".join(myList)
0 Kudos