0

I am writing a python script using version 2.7.3. In the script a line is

toolsDir = 'tools/'

When I run this in terminal I get SyntaxError: invalid syntax on the last character in the string 'r'. I've tried renaming the string, using " as opposed to '. If I actually go into python via bash and declare the string in one line and print it I get no error.

I checked the encoding via file -i update.py and I get text/x-python; charset=us-ascii

I have used TextWrangler, nano and LeafPad as the text editors.

I have a feeling it may be something with the encoding of one of the editors. I have had this script run before without any errors.

Any advice would be greatly appreciated.

KDEx
  • 3,505
  • 4
  • 31
  • 39
  • how are you executing the script ? – karthikr Nov 27 '13 at 15:52
  • This looks vaguely like a DOS line ending issue, with 'r' being mysteriously reported as the final character in the string, but I'm unaware of a reason Python would care, or how exactly a literal carriage return would be turned into a `\r` that the error might be referencing. – chepner Nov 27 '13 at 15:54
  • What do the previous lines look like? – tripleee Nov 27 '13 at 15:58
  • @karthikr from bash (debian linux) python update.py – KDEx Nov 27 '13 at 15:58
  • @chepner I'm running debian linux(and also get the same error on osx), I've tried renaming it just tools and get the same error – KDEx Nov 27 '13 at 15:59
  • @tripleee This is the first line in my script after the #!/usr/bin/python and imports section – KDEx Nov 27 '13 at 16:00

3 Answers3

1

The string is 'tools/'. toolsDir is a variable. You're free to use different terminology, of course, but you'll end up confusing people trying to help you. The only r in that line is the last character of the variable name, so I assume that's the location of the error.

Most likely you've managed to introduce a fixed-width space (character code 0xA0) instead of an ordinary space. Try deleting SP=SP (all three characters) and retyping them.

rici
  • 234,347
  • 28
  • 237
  • 341
  • I have renamed the string toolsDirectory and have backspaced the whole line and rewritten it and got the same error. Could you help me spot this if I posted relevant lines from a hexdump -C output? – KDEx Nov 27 '13 at 16:16
1

Try running the code through pylint.

You probably have a syntax error on a nearby line before this one. Try commenting this line out and see if the error moves.

You might have a whitespace error, don't forget whitespace counts in python. If you've mixed tabs and spaces anywhere in your file it can throw the syntax checker off by several lines.

If you copied and pasted lines into this from any other source you may have copied whitespace in that doesn't fit with whichever convention you used.

cs_alumnus
  • 1,579
  • 16
  • 24
  • This is a good idea that I didn't think of, however the output is that I have a syntax error on the same line. We now that the error isn't elsewhere at least. – KDEx Nov 27 '13 at 16:16
1

The error was, of course, a silly one.

In one of my imports I use try: without closing or catching the error condition. pylint did not catch this and the error message did not indicate this.

If someone in the future has this triple check all opening code for syntax errors.

KDEx
  • 3,505
  • 4
  • 31
  • 39