user7835662
user7835662

Reputation: 1

TypeError: coercing to Unicode: need string or buffer, file found in python(writing data from existing files into single file)

I am supposed to write the data from three existing files to a single file in python. I faced the error "TypeError: coercing to Unicode: need string or buffer", file found. My three existing files are e, g and m and I made a file named results for writing my data from those three mentioned existing files. I really appreciate for any help

filenames= [e,g,m] with open(results, "w") as outfile: for file in filenames: with open(file) as infile: for line in infile: outfile.write(line)

Upvotes: 0

Views: 1824

Answers (1)

buxizhizhoum
buxizhizhoum

Reputation: 1929

Your filename should be a string. Filename e, m, g should be "e", "m", "g", result should be "result". Refer to code below:

#!/usr/bin/python
# -*- coding: utf-8 -*-

filenames= ["e","g","m"]

with open("results", "w") as outfile:
    for file in filenames:
        with open(file) as infile:
            for line in infile:
                outfile.write(line)

Upvotes: 0

Related Questions