Python:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到模块

时间:2016-04-03 03:53:10

标签: python file-io typeerror

我是Python的新手,只是在学习。

我正在将其中一个文件作为输入,我想使用python在控制台上打印文本。

# This will take the file as input

import fileinput
for line in fileinput.input():

    print "The file name you provided is " + fileinput.filename()

    #file of = open(fileinput, "r", 0)
    with open(fileinput,'r') as myfile:
        data=myfile.read()
        print "This is your actual data \n\n" + data

我遇到的错误是:

TypeError: coercing to Unicode: need string or buffer, module found

请有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这个特殊问题如下:

with open(fileinput,'r') as myfile:

fileinput是一个模块。我认为你的意思是:

with open(fileinput.filename(),'r') as myfile: