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
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
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.
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()