Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'parser': 0.07; 'test,': 0.07; 'none):': 0.09; 'prefix': 0.09; 'subject:Why': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; '"to': 0.16; "'w',": 0.16; 'blocks': 0.16; 'false)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'interpreter,': 0.16; 'script,': 0.16; 'syntaxerror:': 0.16; 'tempfile': 0.16; 'used:': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'header :In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'lines': 0.31; '"",': 0.31; 'file': 0.32; 'run': 0.32; 'text': 0.33; 'copying': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'right?': 0.36; 'so,': 0.37; 'pm,': 0.38; 'delete': 0.39; 'blank': 0.60; 'first': 0.61; 'invalid': 0.68; 'subject:get': 0.81; '2015': 0.84; 'pasting': 0.84; 'why?': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=/4CYo9b3Q+ZyzoRquEK0l4Znr5TlDmVW99c3WRpllSc=; b=OVp16qjcJV6WcCi/XcIvi7QBhRNcoBnDaTPRQxgRkAIY4W3C4FGhYV4U9dMqumHdTX ErrmuleXqjMUoTOkrBfmZvLwEd02Ayg+RljB2M/PcdGQOUpXpL1ONaij37DmvuBqCws9 8EwTWGlHfa004mUfLw6/aExIbcYMY2HAySbWycY/fxn21gKK3QMvNJM3Osejcn9R15a/ tIPG+lh0hG3eXbBid9OfEqgqIJQPwhPww2Je7tMrANgFV2HT+pAaXD55Ej5r+TG2MA77 F6o35KmoAeLEjN3NdtV/j6+XEtb4enxes6e26+D2c2a5fB8CZ6t+rRvYnNRLHcMeq201 dfbg== MIME-Version: 1.0 X-Received: by 10.43.39.1 with SMTP id tk1mr25260820icb.26.1430741262025; Mon, 04 May 2015 05:07:42 -0700 (PDT) In-Reply-To: <87d22gk29n.fsf@Equus.decebal.nl> References: <87d22gk29n.fsf@Equus.decebal.nl> Date: Mon, 4 May 2015 22:07:41 +1000 Subject: Re: Why do I get SyntaxError: invalid syntax From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430741264 news.xs4all.nl 2864 [2001:888:2000:d::a6]:34165 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89898 On Mon, May 4, 2015 at 9:31 PM, Cecil Westerhof 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 "", line 6 > rename(tempfile, real_file) > ^ > SyntaxError: invalid syntax > > Why? To clarify: When you say "to test", you mean the interactive interpreter, right? 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. ChrisA