2

My code:

out=open('ab.txt','w')
print("Norwegian Blues stun easily.", file=out)

when I am doing this it is giving syntax error in the second line at "file=out" what to do please help

thanks

1
  • 1
    Python 3 is run with "python3". "python" runs Python 2. Commented Jun 25, 2011 at 4:37

2 Answers 2

2

Have you verified that this is not accidentally running in an earlier version of Python, like as a result of running different versions side-by-side? This syntax is unavailable before 3.x. If you're running this from the interpreter, it should mention what version it is running as at startup, also you should be able to run the command (outside the interpreter)

python --version

to see what the system is defaulting to.

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

1 Comment

No verification needed, this is the problem, with no doubt. :-)
0

Ignoring your exact question, do you mind doing something like this:

out_file = open("test.txt", "wt")
out_file.write("This Text is going to out file\nLook at it and see!")
out_file.close()

I found that here.

EDIT: Alternatively, this code snippet is more along the lines of what ever you're up to:

with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):
    print('B')

I found THAT within this page.

EDIT2: Okay, this may be even more helpful (from here):

#stdout.py
import sys

print 'Dive in'                                          
saveout = sys.stdout                                     
fsock = open('out.log', 'w')                             
sys.stdout = fsock                                       
print 'This message will be logged instead of displayed' 
sys.stdout = saveout                                     
fsock.close()            

Comments

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.