1

I have an invalid syntax according to flake8, pylint, but the code works. What's wrong with that code?

I did lots of Google search but couldn't find anything.

#!/usr/bin/env python
with open("test.py", "a") as output:
    # E:  4, 0: invalid syntax (<string>, line 4) (syntax-error)
    print("hello world", file=output)
1
  • Are you sure you are running the linters for 3 and not those for 2? Under 2, due to the print statement, it is an error for the same reason that (1, a=2) is an error because tuples cannot have key value pairs. Only argument lists can. Commented Jun 9, 2019 at 2:03

1 Answer 1

1

What version of Python are you running? I'm not sure when it was implemented exactly but I don't think earlier versions of Python had a file=output parameter for the print() function so your interpreter might only be expecting a string

Sign up to request clarification or add additional context in comments.

2 Comments

I am using Python 2.7.15
Ah ok, well I think that's the problem - after some googling, it seems like there are two ways to handle it: 1) If you have access, use from __future__ import print_function which allows you to use the print() function from Python 3.x in your Python 2.7 code, or: 2) Apparently this would also write to a file, though I don't have Python 2.7 installed so I can't verify: print >>output, 'This is a test' (replace the 'this is a test' with your text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.