Solved

Redirect "print" statements to a file

Posted on 2009-07-15
9
234 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
  • 5
  • 4
9 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:justearth
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks,
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now