Solved

Redirect "print" statements to a file

Posted on 2009-07-15
9
240 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why does this python script not log into my website? 12 66
Using bootstrap with french text 13 91
python sqlite question 11 46
Edit linux file using python 4 50
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

776 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