Issue:
While writing a script below to a log file:
with open(‘testfile.txt’, ‘wt’) as f: print(‘Hello to the logs’, file=f) with open(‘testfile.txt’, ‘rt’) as f: for line in f: print(‘This is what we printed to the log file : ‘ + line) |
I was repeatedly getting the following error:
D:\Python\Scripts>python printtofile.py File "printtofile.py", line 6 print(‘Hello to the logs’, file=f) ^ SyntaxError: invalid syntax |
Resolution:
Source: http://docs.python.org/2/library/functions.html#print
from __future__ import print_function with open(‘testfile.txt’, ‘wt’) as f: print(‘Hello to the logs’, file=f) with open(‘testfile.txt’, ‘rt’) as f: for line in f: print(‘This is what we printed to the log file : ‘ + line) |