Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89895 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-05-04 13:31 +0200 |
| Last post | 2015-05-04 15:16 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
Why do I get SyntaxError: invalid syntax Cecil Westerhof <Cecil@decebal.nl> - 2015-05-04 13:31 +0200
Re: Why do I get SyntaxError: invalid syntax Chris Angelico <rosuav@gmail.com> - 2015-05-04 22:07 +1000
Re: Why do I get SyntaxError: invalid syntax Cecil Westerhof <Cecil@decebal.nl> - 2015-05-04 15:16 +0200
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-05-04 13:31 +0200 |
| Subject | Why do I get SyntaxError: invalid syntax |
| Message-ID | <87d22gk29n.fsf@Equus.decebal.nl> |
While copying pasting code to test, the following works:
from itertools import islice
from os import rename
from os.path import expanduser, split
from tempfile import NamedTemporaryFile
real_file = (expanduser('~/Twitter/testing.txt'))
(filepath,
file) = split(real_file)
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 first I used:
from itertools import islice
from os import rename
from os.path import expanduser, split
from tempfile import NamedTemporaryFile
real_file = (expanduser('~/Twitter/testing.txt'))
(filepath,
file) = split(real_file)
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?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-05-04 22:07 +1000 |
| Message-ID | <mailman.82.1430741264.12865.python-list@python.org> |
| In reply to | #89895 |
On Mon, May 4, 2015 at 9:31 PM, Cecil Westerhof <Cecil@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? 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
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-05-04 15:16 +0200 |
| Message-ID | <87zj5kiitz.fsf@Equus.decebal.nl> |
| In reply to | #89898 |
Op Monday 4 May 2015 14:07 CEST schreef Chris Angelico: > On Mon, May 4, 2015 at 9:31 PM, Cecil Westerhof <Cecil@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web