TypeError: coercing to Unicode: need string or buffer
You’re trying to open each file twice! First you do: and then you pass infile (which is a file object) to the open function again: open is of course expecting its first argument to be a file name, not an opened file! Open the file once only and you should be fine.[