Why do I get SyntaxError: invalid syntax

Cecil Westerhof Cecil at decebal.nl
Mon May 4 09:16:24 EDT 2015


Op Monday 4 May 2015 14:07 CEST schreef Chris Angelico:

> On Mon, May 4, 2015 at 9:31 PM, Cecil Westerhof <Cecil at decebal.nl> wrote:
>> While copying pasting code to test, the following works: [chomp]
>> But first I used: with NamedTemporaryFile(mode = 'w', prefix = file
>> + '_', dir = filepath, delete = False) as tf: tempfile = tf.name
>> with open(real_file, 'r') as f: for line in islice(f, 1, None):
>> tf.write(line) rename(tempfile, real_file)
>>
>> But that gave:
>> File "<stdin>", line 6
>> rename(tempfile, real_file)
>> ^
>> SyntaxError: invalid syntax
>>
>> Why?
>
> To clarify: When you say "to test", you mean the interactive
> interpreter, right?

Yes, that is what I mend. Should have been clearer.


> If so, you need to end blocks of text with blank
> lines (and not have any blank lines in between). It's because the
> parser has to know when to run stuff; when you run a script, it
> parses the whole thing and then runs it, but interactively, it has
> to work piece-meal.

OK, thanks: I understand it now.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list