Why do I get SyntaxError: invalid syntax

Cecil Westerhof Cecil at decebal.nl
Mon May 4 07:31:16 EDT 2015


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



More information about the Python-list mailing list