Python: Print to a log error


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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s