Solved

Print to screen and save output to file

Posted on 2009-07-16
6
783 Views
Last Modified: 2012-05-07
Hello,

When I run this script now I do so from the command line like this:

c:\python24\python.exe "C:\getgridprop.py" "c:\tmin\tminpro" > c:\tmin\tmin_avg.txt

This saves the output but prints nothing to screen. As I easily get anxious is there to way save the output and print to screen? This way I'll know its working.  Also, is there a way to save only what what the "print" command outputs?  I'd like to uncomment the lines below and see them on screen but not saved to the output file.

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 RasterImage, 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
  • 3
  • 3
6 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 435 total points
ID: 24871439
You could do something like below. Instead of redirecting standard output, write to a specific file. The name of the file is provided as the last parameter on the command line:

c:\python24\python.exe "C:\getgridprop.py" "c:\tmin\tminpro" c:\tmin\tmin_avg.txt

import arcgisscripting, sys, string, os
gp = arcgisscripting.create()
gp.overwriteoutput = 1
 
gp.workspace = sys.argv[1]
InFolder = sys.argv[1]
 
Outfile = open(sys.argv[2],'w')
 
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)
      Outfile.write(RasterImage + " " + str(zmax) + "\n")
      gp.AddMessage(RasterImage + " Properties have been collected.")
      gp.AddMessage("\n")
      RasterImage = RasterClassList.Next()
 
except:
    # Print error message if an error occurs 
    print gp.GetMessages()
 
Outfile.close()

Open in new window

0
 

Author Comment

by:justearth
ID: 24871493
cxr:

I get the following error message:

C:\Documents and Settings\Owner>c:\python24\python.exe "C:\Documents and Setting
s\Owner\Desktop\getgridprop2.py" "c:\downloads\tmin\tminpro" > c:\downloads\tmin
_avg.txt
Traceback (most recent call last):
  File "C:\Documents and Settings\Owner\Desktop\getgridprop2.py", line 8, in ?
    Outfile = open(sys.argv[2],'w')
IndexError: list index out of range

Please advise,
Thanks again,
JE
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24871505
Remove the > character:

c:\python24\python.exe "C:\getgridprop.py" "c:\tmin\tminpro" c:\downloads\tmin_avg.txt
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:justearth
ID: 24871616
Thanks this worked.  Does the 'w' in this line -- Outfile = open(sys.argv[2],'w') -- mean that this file can be written in? And what would happen if you didn't include the 'str' in this line: Outfile.write(RasterImage + " " + str(zmax) + "\n")

Thanks again,
JE

0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 435 total points
ID: 24871745
Yes, the 'w' means to open the file i write mode.

>> what would happen if you didn't include the 'str'

If zmax is an integer, you would get the error "TypeError: cannot concatenate 'str' and 'int' objects". If it is a float, you would get the error "TypeError: cannot concatenate 'str' and 'float' objects".
0
 

Author Closing Comment

by:justearth
ID: 31604321
thanks again.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
INSERT for sqlite3 in Python 9 86
Python - ImportError: No module named 'urllib2' 2 204
Python On Mouseover Save URL/Link 4 63
Old GPS chip on Samsung Tab 4 - Kit Kat? 2 38
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,…
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…
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…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

813 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

15 Experts available now in Live!

Get 1:1 Help Now