python - Why is there SyntaxError to this equal sign when printing to a file? -


i'm using terminal on macbook print data open file:

>>> out=open("test_output.txt","w") >>> print("hello",file=out)   file "<stdin>", line 1     print("hello",file=out)                       ^ syntaxerror: invalid syntax 

why there syntaxerror , how fix it? anyway, same scripts runs in idle.

ps:

it's python 2.7, i've installed python 3.5, packages of networkx , matplotlib both automatically installed libraries of python 2.7, that's platform i'm using while doing social network analysis.

before begin answer question regarding syntax error, first need tell there two versions of python. python 2 , python 3. python 3 supposed future of language , version running in idle installation. python 2 version using when invoke python in command line.

there isn't of difference between two, print 1 of them. print function in python 3, statement in python 2. mean? in python 2, print not return anything, pushes data out command line. in python 3, returns something. means in python 3, can this:

a = print("thing") 

in python 2, if same thing, syntax error:

>>> = print("thing")   file "<input>", line 1     = print("thing")             ^ syntaxerror: invalid syntax 

because print function in python 3, can provide additional arguments. why can print("thing", out=file). in python 2, equivalent print>>file, "thing".

so, have few options now. can change .py file reflect correct syntax in python 2. can use python 3 run file instead of python 2 using python3 invoke python in command line.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -