Solved

Redirect "print" statements to a file

Posted on 2009-07-15
9
241 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
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

830 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