打开数据文件以读取该文件中包含的行

时间:2018-01-21 23:22:50

标签: python python-2.7

我使用以下代码段将数据文件分为两部分:

def shuffle_split(infilename, outfilename1, outfilename2):

    with open(infilename, 'r') as f:
        lines = f.readlines()

    lines[-1] = lines[-1].rstrip('\n') + '\n'

    shuffle(lines)

    with open(outfilename1, 'w') as f:
        f.writelines(lines[:90000])
    with open(outfilename2, 'w') as f:
        f.writelines(lines[90000:])
    outfilename1.close()
    outfilename2.close()    
shuffle_split(data_file, training_file,validation_file)

运行此代码段会导致以下错误,

in shuffle_split
with open(infilename, 'r') as f:
TypeError: coercing to Unicode: need string or buffer, file found

打开data_file进行输入的方式有什么问题?

1 个答案:

答案 0 :(得分:1)

infilename传入的内容已经是file,而不是文件的路径名。