tags:

views:

549

answers:

6

i have this python script where i need to run 'gdal_retile.py'

but i get this an exception on this line:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

the end='' is invalid syntax just curious as to why.. and what the author probably meant to do.

I'm new to python if you haven't already guessed.

Edit: Added the opening quote. that was just a typo*


I think the root cause of the problem is that these imports are failing and therefore one must contain this import from __future__ import print_function

try: 
   from osgeo import gdal
   from osgeo import ogr
   from osgeo import osr
   from osgeo.gdalconst import *
except:
   import gdal
   import ogr
   import osr
   from gdalconst import *

Thanks all.

+4  A: 

How about this:

#Only for use in Python 2.6.0a2 and later
from __future__ import print_function

This allows you to use the Python 3.0 style print function without having to hand-edit all occurrences of print :)

badp
+1 for the `↑` character
Joachim Sauer
@bp. sorry that was just a typo. but nice up arrow
Hath
I had to remove it :(
badp
+1  A: 

I think the author probably meant:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')

He's missing an initial quote after print(.

Note that as of Python 3.0, print is a function as opposed to a statement, if you're using older versions of Python the equivalent would be:

print "Building internam Index for %d tile(s) ..." % len(inputTiles)

The end parameter means that the line gets ' ' at the end rather than a newline character. The equivalent in earlier versions of Python is:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),

(thanks Ignacio).

Dominic Rodger
Adding a `,` to the end will do it.
Ignacio Vazquez-Abrams
+2  A: 

It looks like you're just missing an opening double-quote. Try:

if Verbose:
   print("Building internam Index for %d tile(s) ..." % len(inputTiles), end=' ')
Will Robinson
+1  A: 

I think he's using Python 3.0 and you're using Python 2.6.

Charles Beattie
Good point! `print("foo" % bar, end = " ")` triggers a syntax error in Python 2.6 under `end = " "` just like the missing quote does :)
badp
+4  A: 

First of all, you're missing a quote at the beginning but this is probably a copy/paste error.

In Python 3.x, the end=' ' part will place a space after the displayed string instead of a newline. To do the same thing in Python 2.x, you'd put a comma at the end:

print "Building internam Index for %d tile(s) ..." % len(inputTiles),
interjay
+2  A: 

Are you sure you are using Python 3.x? The syntax isn't available in Python 2.x because print is still a statement.

print("foo" % bar, end=" ")

in Python 2.x is identical to

print ("foo" % bar, end=" ")

or

print "foo" % bar, end=" "

i.e. as a call to print with a tuple as argument.

That's obviously bad syntax (literals don't take keyword arguments). In Python 3.x print is an actual function, so it takes keyword arguments, too.

The correct idiom in Python 2.x for end=" " is:

print "foo" % bar,

(note the final comma, this makes it end the line with a space rather than a linebreak)

If you want more control over the output, consider using sys.stdout directly. This won't do any special magic with the output.

Of course in somewhat recent versions of Python 2.x (2.5 should have it, not sure about 2.4), you can use the __future__ module to enable it in your script file:

from __future__ import print_function

The same goes with unicode_literals and some other nice things (with_statement, for example). This won't work in really old versions (i.e. created before the feature was introduced) of Python 2.x, though.

Alan