127

I have this python script where I need to run gdal_retile.py, but I get an exception on this line:

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

The end=' ' is invalid syntax. I am curious as to why, and what the author probably meant to do.

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


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 *
martineau
  • 119,623
  • 25
  • 170
  • 301
Hath
  • 12,606
  • 7
  • 36
  • 38
  • 2
    It really helps if you post the traceback to best diagnose the exception you receive. The obvious syntax error is from the lack of opening quotes. If that was fixed, it would still be a syntax error in Python 2, which does not have the print function without a `__future__` import. – Mike Graham Mar 16 '10 at 16:36
  • 2
    As am aside, variables that are not `ClassNames` should begin with a lower-case letter. – Mike Graham Mar 16 '10 at 16:37
  • wrt/ @Mike, see http://www.python.org/dev/peps/pep-0008/ for the complete guidelines for variable naming and coding style in Python. It's a good idea to adhere to the guidelines because in Python you have the privilege of working with a mostly consistent library and can thus often avoid the usual guessing game (e.g. PHP) even when you're working with other people's code. – Alan Plum Mar 16 '10 at 16:54
  • 6
    When asking for help with errors in the future, *especially syntax errors*, you should try to provide the *exact code you've tried running*, without any retyping. – Mike Graham Mar 16 '10 at 17:21
  • @Mike - I usually would never bother in typing it out again, however i was unable to do so as the code was on a remote computer. I'll be sure to be more careful in the future. – Hath Mar 18 '10 at 10:26

17 Answers17

161

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alan Plum
  • 10,814
  • 4
  • 40
  • 57
  • 1
    I checked mine version using `dpkg -p python` and it was version 2.xx.xx. Thank you so much, I applied the suggestion to put it in the form of `print "foo" %bar, ` and it worked perfectly fine. – Mehrad May 05 '14 at 23:20
  • Keep in mind, there is a (subtle) difference between `print foo, bar` and `print (foo, bar)` in Python 2. The former is a `print` statement with two items in its "argument" list; the latter is a `print` statement with a single tuple in its argument list. – chepner Aug 11 '17 at 12:42
  • Your example `print("foo" % bar, end=" ")` won't work, since `bar` is not defined; so what is `bar` in your example? (At least for me with Python 3.7) – Qaswed Apr 05 '19 at 11:27
52

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
  • 11,409
  • 3
  • 61
  • 89
17

In python 2.7 here is how you do it

mantra = 'Always look on the bright side of life'
for c in mantra: print c,

#output
A l w a y s   l o o k   o n   t h e   b r i g h t   s i d e   o f   l i f e

In python 3.x

myjob= 'hacker'
for c in myjob: print (c, end=' ')
#output 
h a c k e r 
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17
  • 5
    Man, you just solve a problem confusing me for couple days, i just remember those whistle and old-day scene but not the lyris, and now i finally figure out what that song is!! – Sphynx-HenryAY Mar 10 '17 at 20:01
  • @Sphynx-HenryAY happy to help you out brother – Ritesh Karwa Mar 10 '17 at 20:40
  • On your second example, it appears that `h a c k e r ` has spaces in-between characters. I'm using Python 3.7.3 and I get no spaces at all with `end=''`. – Nagev Jul 03 '19 at 16:51
  • @Nagev just put a space between ' ' of end=' ' – Ritesh Karwa Jul 03 '19 at 18:46
  • Thanks, I do *not* want the space, it's just that I thought that I saw your code **without** the space yet the output was spaced, which seemed inconsistent, that's all. – Nagev Jul 04 '19 at 07:52
6

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
  • 107,303
  • 21
  • 270
  • 254
  • I'm intersted to do something like this in python 2 what would be the syntax ? what do you mean add a comma ? – 0xtuytuy Sep 23 '15 at 09:35
  • 1
    @Rémi The code in my answer is in Python 2... And I'm referring to the comma at the end. – interjay Sep 23 '15 at 14:56
4

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

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
Charles Beattie
  • 5,739
  • 1
  • 29
  • 32
  • 1
    Good point! `print("foo" % bar, end = " ")` triggers a syntax error in Python 2.6 under `end = " "` just like the missing quote does :) – badp Mar 16 '10 at 16:33
3

This is just a version thing. Since Python 3.x the print is actually a function, so it now takes arguments like any normal function.

The end=' ' is just to say that you want a space after the end of the statement instead of a new line character. In Python 2.x you would have to do this by placing a comma at the end of the print statement.

For example, when in a Python 3.x environment:

while i<5:
    print(i)
    i=i+1

Will give the following output:

0
1
2
3
4

Where as:

while i<5:
    print(i, end = ' ')
    i=i+1

Will give as output:

0 1 2 3 4
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • A link to the official documentation of `print()` might be helpful, since there one can see the default value `'\n'` for the argument `end`: https://docs.python.org/3/library/functions.html#print – Qaswed Apr 05 '19 at 11:40
  • Don't forget a space in the end like the example above: "h a c k e r " the output here should be "0 1 2 3 4 " with a last space correspondingly – alant Jul 26 '19 at 22:45
2

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
  • 2,169
  • 1
  • 13
  • 8
2

Compatible with both Python 2 & 3:

sys.stdout.write('mytext')

Compatible with only Python 2

print 'mytext',

Compatible with only Python 3

print('mytext', end='')
JellicleCat
  • 28,480
  • 24
  • 109
  • 162
1

USE :: python3 filename.py

I had such error , this occured because i have two versions of python installed on my drive namely python2.7 and python3 . Following was my code :

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when i run it by the command python lines.py I got the following error

#!usr/bin/python

f = open('lines.txt')
for line in f.readlines():
        print(line,end ='')

when I run it by the command python3 lines.py I executed successfully

Subbu
  • 2,063
  • 4
  • 29
  • 42
1

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
  • 97,747
  • 36
  • 197
  • 212
1

For python 2.7 I had the same issue Just use "from __future__ import print_function" without quotes to resolve this issue.This Ensures Python 2.6 and later Python 2.x can use Python 3.x print function.

1

Try this one if you are working with python 2.7:

from __future__ import print_function
jwpfox
  • 5,124
  • 11
  • 45
  • 42
scofield
  • 11
  • 1
  • Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar or using Ctrl+K on your keyboard to nicely format and syntax highlight it! – WhatsThePoint Dec 21 '17 at 08:04
1

Basically, it occurs in python2.7 here is my code of how it works:

  i=5
while(i):
    print i,
    i=i-1
Output:
5 4 3 2 1
1

I had faced this error, and occurred because of using version 2.7.15 but end='' works with version 3 only.

Example:

import array as arr
a=arr.array('i',[1,2,3,4])
for x in range(0,3):
    print ("%d" % a[x], end=' ')

Output : Showing error "Invalid Syntax" as using version 2.7

For Version 2.7.15 use this syntax

import array as arr
a=arr.array('i',[1,2,3,4])
for x in range(0,3):
    print ("%d" % a[x]),

Output : 1 2 3

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
0

Even I was getting that same error today. And I've experienced an interesting thing. If you're using python 3.x and still getting the error, it might be a reason:

You have multiple python versions installed on same drive. And when you're presing the f5 button the python shell window (of ver. < 3.x) pops up

I was getting same error today, and noticed that thing. Trust me, when I execute my code from proper shell window (of ver. 3.x), I got satisfactory results

biswajit
  • 2,707
  • 4
  • 17
  • 16
0

we need to import a header before using end='', as it is not included in the python's normal runtime.

from __future__ import print_function

it shall work perfectly now

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sukantk
  • 39
  • 4
0

Just for my own future reference (I've come here twice already at different times), If you check and you're definitely on a python 3.x or later and you're getting the error telling you the 'end=' syntax is incorrect: Make sure that you're actually running the python script and not just trying to execute the file. I have done this in the heat of a moment and thought something was terribly wrong only to discover I was doing just that.

python "scriptName.py"

or

python3 "scriptName.py"
Matt Binford
  • 620
  • 8
  • 15