You're not formatting the string correctly, fix this line:
f.write("I am writing now to the file %s \n" % f.name)
On your code, you closed the parenthesis leaving the string unformatted and with a SyntaxError since % (f.name)
is not valid Python.
By the way, that kind of formatting is been improved with str.format()
. Something like this:
f.write("I am writing now to the file {} \n".format(f.name))