Solved

Redirect "print" statements to a file

Posted on 2009-07-15
9
251 Views
Last Modified: 2012-05-07
Hello,
Please see the script below. I'd like to save to file the the 'zmax' output plus the name of the raster image on a single line. Each iteration would be the next line in the same out file. Can this script be modified to do so? If so how?

e.g.

Name1 100
Name2 101
etc

Currently the output to screen looks like this:
C:\Documents and Settings\u4cncmgj>c:\python24\python.exe "C:\Documents and Sett
ings\u4cncmgj\Desktop\getgridprop.py" "C:\GISWORK\NEW_WORK\delete_dem"

Getting Raster Properties
Working on delete1dem
delete1dem Properties have been collected.


Executing: GetRasterProperties C:\GISWORK\NEW_WORK\delete_dem/delete1dem MEAN 0
Start Time: Wed Jul 15 17:36:19 2009
Mean = 357.948876
Executed (GetRasterProperties) successfully.
End Time: Wed Jul 15 17:36:19 2009 (Elapsed Time: 0.00 seconds)

Thanks,
JE


import arcgisscripting, sys, string, os
gp = arcgisscripting.create()
gp.overwriteoutput = 1
 
gp.workspace = sys.argv[1]
InFolder = sys.argv[1]
 
 
try:
    RasterClassList = gp.ListRasters()
    RasterClassList.Reset()
    RasterImage = RasterClassList.Next()
    gp.AddMessage("\n" + "Getting Raster Properties")
    while RasterImage:
      gp.AddMessage("Working on " + RasterImage)
      InFileName = InFolder + "/" + RasterImage
      InPropertyType = "MEAN"
      zmax = gp.GetRasterProperties (InFileName, InPropertyType)
      print zmax
      gp.AddMessage(RasterImage + " Properties have been collected.")
      gp.AddMessage("\n")
      RasterImage = RasterClassList.Next()
 
except:
    # Print error message if an error occurs 
    print gp.GetMessages()

Open in new window

0
Comment
Question by:justearth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24865294
Maybe you can just send standard out to a file using " > output.txt", like this:
C:\Documents and Settings\u4cncmgj>c:\python24\python.exe "C:\Documents and Settings\u4cncmgj\Desktop\getgridprop.py" "C:\GISWORK\NEW_WORK\delete_dem" > output.txt

Open in new window

0
 

Author Comment

by:justearth
ID: 24865340
This works as in outputs the "print", but the output is a bit messy and will probably be headache like, given I have 1000's iterations and limited python knowledge I am afraid this method will create more headache for me.

Thanks,
JE
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24865397
I don't know what you mean... You wanted the output in a file, redirecting standard out works, but the output is messy? Would it be less messy if you created the output in a different way?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:justearth
ID: 24865441
I guess I was hoping  to have the output like I requested in the first post:

Name1 100
Name2 101


But the when I paste the output to anyother place it puts it out of order.  I can deal with it, I was hoping there was a slicker way.

Cheers,
JE
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24865582
Where does "Name1" and "Name2" come from, and what is "100" and "101"? The output you are getting does not look like the output you want at all... I don't understand how I can help you getting the output you want, please explain.
0
 

Author Comment

by:justearth
ID: 24865836
cxr:
It was not my intention to be so unclear.  

name1 100 was supposed to be a generic name to illustrate my point. From what I included above:

delete1dem 357.948876
 
This would be the desired output.  delete1dem comes from RasterImage and 357.948876 is the result from zmax.  Given that I am unsure on how to get the info I want from within ESRI ArcMap coding (e.g. If I print RasterImage it include other infor from within the software and will not finish the loop).  I will say your method for output will do me fine for now.]


Thanks,
JE
0
 

Author Closing Comment

by:justearth
ID: 31604037
Thanks,
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24865973
I'm not sure if this helps, but try to remove the calls to gp.AddMessage(), and print the RasterImage and zmax variables in the loop:
    RasterClassList = gp.ListRasters()
    RasterClassList.Reset()
    RasterImage = RasterClassList.Next()
    #gp.AddMessage("\n" + "Getting Raster Properties")
    while RasterImage:
      #gp.AddMessage("Working on " + RasterImage)
      InFileName = InFolder + "/" + RasterImage
      InPropertyType = "MEAN"
      zmax = gp.GetRasterProperties (InFileName, InPropertyType)
      print RasterImage, zmax
      #gp.AddMessage(RasterImage + " Properties have been collected.")
      #gp.AddMessage("\n")
      RasterImage = RasterClassList.Next()

Open in new window

0
 

Author Comment

by:justearth
ID: 24866426
Yes, this worked great.

Before I was using:

print RasterImage + zmax

and that was giving me some crazy stuff. This worked swell.

Cheers,
JE
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I feel like more and more people want to know how to programmatically convert addresses into geospatial locations. So in this article, I will show you how you can do it with Bing Maps. I'm going to use PowerShell, which is a nice scripting language,…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question