2017-07-26 47 views
0

我应该将三个现有文件中的数据写入python中的单个文件。我遇到了错误“TypeError:强制为Unicode:需要字符串或缓冲区”,发现文件。我的三个现有文件是e,g和m,并且我从这三个现有文件中创建了一个名为结果的文件,用于写入我的数据。非常感谢您的帮助TypeError:强制为Unicode:需要字符串或缓冲区,在Python中找到文件(将数据从现有文件写入单个文件)

文件名= [E,G,M] 张开(结果, “W”)为OUTFILE: 为文件中的文件名: 具有开放(文件)作为infile中: 为线in infile: outfile.write(line)

回答

0

你的文件名应该是一个字符串。 文件名e,m,g应该是“e”,“m”,“g”,结果应该是“结果”。 请参考以下编号:

#!/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) 
相关问题